반응형
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerShooting4 : MonoBehaviour { public Transform gunBarrelEnd; private LineRenderer gunLine; private Light gunLight; public float power = 10000f; public float range = 100f; public float timeBetweenBullets = 0.15f; public float effectsDisplayTime = 0.05f; private Ray ray = new Ray(); private RaycastHit hit; private float timer = 0f; private int layerMask; public void Awake() { gunLine = GetComponentInChildren<LineRenderer>(); gunLight = GetComponentInChildren<Light>(); layerMask = LayerMask.GetMask("Shootable"); } public void Update() { timer += Time.deltaTime; if (Input.GetButton("Fire1") && timer > timeBetweenBullets) { timer = 0f; Shoot(); } if (timer > effectsDisplayTime) DisableEffects(); } private void Shoot() { gunLine.SetPosition(0, gunBarrelEnd.position); ray.origin = gunBarrelEnd.position; ray.direction = gunBarrelEnd.forward; if (Physics.Raycast(ray, out hit, range, layerMask)) { gunLine.SetPosition(1, hit.point); if (hit.collider != null) { if (hit.collider.name.Contains("Cube")) { var rb = hit.collider.GetComponent<Rigidbody>(); if (rb != null) rb.AddForce(gunBarrelEnd.forward * power); } } } else { var goalPosition = gunBarrelEnd.position + (gunBarrelEnd.forward * range); gunLine.SetPosition(1, goalPosition); } EnableEffects(); } private void EnableEffects() { gunLine.enabled = true; gunLight.enabled = true; } private void DisableEffects() { gunLine.enabled = false; gunLight.enabled = false; } } | cs |
반응형
'etc-posts > Unity :: C# 튜토리얼' 카테고리의 다른 글
[유니티C#][기초] 11. 적에게 체력 부여하기 (0) | 2018.07.08 |
---|---|
[유니티C#][기초] 10.플레이어 슈팅 5 (최종) (0) | 2018.07.08 |
[유니티C#][기초] 8. 플레이어를 따라오는 적 만들기 (0) | 2018.07.08 |
[유니티C#][기초] 7. 플레이어 슈팅 3 (0) | 2018.07.08 |
[유니티C#][기초] 6.플레이어 슈팅 2 (0) | 2018.07.08 |