반응형
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 91 | 2-3 PlayerHealth using UnityEngine; //UI관련 사용하려면 반드시 using using UnityEngine.UI; using System.Collections; using UnityEngine.SceneManagement; public class PlayerHealth : MonoBehaviour { public int startingHealth = 100; public int currentHealth; public Slider healthSlider; public Image damageImage; public AudioClip deathClip; public float flashSpeed = 5f; public Color flashColour = new Color(1f, 0f, 0f, 0.1f); Animator anim; AudioSource playerAudio; PlayerMovement playerMovement; //PlayerShooting playerShooting; bool isDead; bool damaged; void Awake () { anim = GetComponent <Animator> (); playerAudio = GetComponent <AudioSource> (); playerMovement = GetComponent <PlayerMovement> (); //playerShooting = GetComponentInChildren <PlayerShooting> (); currentHealth = startingHealth; } void Update () { if(damaged) { damageImage.color = flashColour; } else { damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime); } damaged = false; } public void TakeDamage (int amount) { damaged = true; currentHealth -= amount; healthSlider.value = currentHealth; playerAudio.Play (); if(currentHealth <= 0 && !isDead) { Death (); } } void Death () { isDead = true; //playerShooting.DisableEffects (); anim.SetTrigger ("Die"); playerAudio.clip = deathClip; playerAudio.Play (); playerMovement.enabled = false; //playerShooting.enabled = false; } public void RestartLevel () { SceneManager.LoadScene (0); } } | cs |
반응형
'etc-posts > Unity :: C# 튜토리얼' 카테고리의 다른 글
[유니티C#][기초] 16. 튜토리얼 SurvivalShooter 정리.5 (0) | 2018.07.14 |
---|---|
[유니티C#][기초] 15. 튜토리얼 SurvivalShooter 정리.4 (0) | 2018.07.14 |
[유니티C#][기초] 13. 튜토리얼 SurvivalShooter 정리.2 (0) | 2018.07.08 |
[유니티C#][기초] 12. 튜토리얼 SurvivalShooter 정리.1 (0) | 2018.07.08 |
[유니티C#][기초] 11. 적에게 체력 부여하기 (0) | 2018.07.08 |