반응형
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 | using System.Collections; using UnityEngine; public class PlayerShooting3 : 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; public void Awake() { gunLine = GetComponentInChildren<LineRenderer>(); gunLight = GetComponentInChildren<Light>(); } 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)) { gunLine.SetPosition(1, hit.point); if (hit.collider != null) { 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); } //이팩터 부분은 따로 함수를 만들어서 , 라인과 라이트를 On/off할수 있는 구조!! 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#][기초] 9. 플레이어 슈팅 4 (0) | 2018.07.08 |
---|---|
[유니티C#][기초] 8. 플레이어를 따라오는 적 만들기 (0) | 2018.07.08 |
[유니티C#][기초] 6.플레이어 슈팅 2 (0) | 2018.07.08 |
[유니티 C#][기초] 5.플레이어 슈팅 1 (0) | 2018.07.08 |
[유니티 C#][기초] 4. 마우스, 키보드 입력 받기 (0) | 2018.07.08 |