Created
January 10, 2021 05:48
-
-
Save fredknack/f483907d2e2caf00a90f07d82c9c913e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Target : MonoBehaviour | |
{ | |
public Color m_FlashDamageColor = Color.white; | |
private MeshRenderer m_MeshRenderer = null; | |
private Color m_OriginalColor = Color.white; | |
private int m_MaxHealth = 2; | |
private int m_Health = 0; | |
public GameObject boomPrefab; | |
private MoveToGoal movementScript; | |
private void Awake() | |
{ | |
m_MeshRenderer = GetComponent<MeshRenderer>(); | |
m_OriginalColor = m_MeshRenderer.material.color; | |
movementScript = GetComponent<MoveToGoal>(); | |
} | |
private void OnEnable() | |
{ | |
ResetHealth(); | |
} | |
private void OnCollisionEnter(Collision collision) | |
{ | |
if (collision.gameObject.CompareTag("Projectile")) | |
Damage(); | |
} | |
private void Damage() | |
{ | |
StopAllCoroutines(); | |
StartCoroutine(Flash()); | |
RemoveHealth(); | |
} | |
private IEnumerator Flash() | |
{ | |
m_MeshRenderer.material.color = m_FlashDamageColor; | |
WaitForSeconds wait = new WaitForSeconds(0.3f); | |
yield return wait; | |
m_MeshRenderer.material.color = m_OriginalColor; | |
} | |
private void RemoveHealth() | |
{ | |
m_Health--; | |
CheckForDeath(); | |
} | |
private void ResetHealth() | |
{ | |
m_Health = m_MaxHealth; | |
} | |
private void CheckForDeath() | |
{ | |
if (m_Health <= 0) | |
{ | |
Kill(); | |
} else | |
{ | |
movementScript.react(); | |
} | |
} | |
private void Kill() | |
{ | |
Instantiate(boomPrefab, transform.position, Quaternion.identity); | |
gameObject.SetActive(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment