반응형
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 84 85 86 87 88 89 90 | 2-4 PlayerShooting using UnityEngine; public class PlayerShooting : MonoBehaviour { public int damagePerShot = 20; public float timeBetweenBullets = 0.15f; public float range = 100f; float timer; Ray shootRay = new Ray(); RaycastHit shootHit; int shootableMask; ParticleSystem gunParticles; LineRenderer gunLine; AudioSource gunAudio; Light gunLight; float effectsDisplayTime = 0.2f; void Awake () { shootableMask = LayerMask.GetMask ("Shootable"); gunParticles = GetComponent<ParticleSystem> (); gunLine = GetComponent <LineRenderer> (); gunAudio = GetComponent<AudioSource> (); gunLight = GetComponent<Light> (); } void Update () { timer += Time.deltaTime; //Time.timescale-> 플레이 시간 조정 -> 일시정지 / 슬로우 무션 / 정상속도 / 2배 속도 가능 if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0) { Shoot (); } if(timer >= timeBetweenBullets * effectsDisplayTime) { DisableEffects (); } } public void DisableEffects () { gunLine.enabled = false; gunLight.enabled = false; } void Shoot () { timer = 0f; gunAudio.Play (); gunLight.enabled = true; gunParticles.Stop (); gunParticles.Play (); gunLine.enabled = true; gunLine.SetPosition (0, transform.position); shootRay.origin = transform.position; shootRay.direction = transform.forward; if(Physics.Raycast (shootRay, out shootHit, range, shootableMask)) { EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> (); if(enemyHealth != null) { enemyHealth.TakeDamage (damagePerShot, shootHit.point); } gunLine.SetPosition (1, shootHit.point); } else { gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range); } } } | cs |
반응형
'etc-posts > Unity :: C# 튜토리얼' 카테고리의 다른 글
[유니티C#][기초] 17. 튜토리얼 SurvivalShooter 정리.6 (0) | 2018.07.14 |
---|---|
[유니티C#][기초] 16. 튜토리얼 SurvivalShooter 정리.5 (0) | 2018.07.14 |
[유니티C#][기초] 14. 튜토리얼 SurvivalShooter 정리.3 (0) | 2018.07.08 |
[유니티C#][기초] 13. 튜토리얼 SurvivalShooter 정리.2 (0) | 2018.07.08 |
[유니티C#][기초] 12. 튜토리얼 SurvivalShooter 정리.1 (0) | 2018.07.08 |