Created
July 24, 2017 19:35
-
-
Save anonymous/5c96a6768ebb88f79adb781eb6bbc190 to your computer and use it in GitHub Desktop.
This file contains 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
public class EnemyAI : MonoBehaviour { | |
private NavMeshAgent agent; | |
[SerializeField] | |
public float healthPoint = 50; | |
private Animator enemyAnimation; | |
private GameObject player; | |
private PlayerAttack playerEnemyList; | |
private bool alive = true; | |
private bool startFade = false; | |
//fading enemy color when die | |
private SkinnedMeshRenderer rend; | |
private Color enemyColor; | |
private Vector3 ranPos; | |
float pauseInterval = 0; | |
float pauseTime = 0; | |
// Use this for initialization | |
void Start () { | |
rend = GetComponentInChildren<SkinnedMeshRenderer> (); | |
enemyColor = rend.material.color; | |
agent = GetComponent<NavMeshAgent> (); | |
enemyAnimation = GetComponent<Animator> (); | |
player = GameObject.FindGameObjectWithTag ("Player"); | |
playerEnemyList = player.GetComponent<PlayerAttack> (); | |
CalculateRandomPos (); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (player != null && alive) { | |
//check if player's nearby | |
if (Vector3.Distance (transform.position, player.transform.position) <= 3) { | |
//move toward player | |
agent.SetDestination (player.transform.position); | |
//pas the movement velocity to animator | |
enemyAnimation.SetFloat ("runningSpeed", agent.velocity.magnitude); | |
} else { | |
//if reached the nearby random position | |
if (Vector3.Distance (transform.position, ranPos) < 3) { | |
//recalculate new ranPos | |
CalculateRandomPos (); | |
//calculate the time to start movin again | |
pauseInterval = Random.Range (2, 10); | |
pauseTime = pauseInterval; | |
} else { | |
//already passed the pausing interval | |
if (pauseTime <= 0) { | |
//move to random nearby location | |
agent.SetDestination (ranPos); | |
//just in case if the AI stuck, recalculate random point | |
if (agent.velocity == Vector3.zero) { | |
CalculateRandomPos (); | |
} | |
} | |
} | |
if (pauseTime >= 0) { | |
pauseTime -= Time.deltaTime; | |
} | |
} | |
} else if (!alive && startFade) { | |
enemyColor = Color.Lerp (enemyColor, new Color (1, 1, 1, 0), 0.1f); | |
rend.material.color = enemyColor; | |
} | |
} | |
public void GetDamage (float attackDamage) | |
{ | |
healthPoint -= attackDamage; | |
if (healthPoint <= 0) { | |
playerEnemyList.RemoveEnemy (gameObject); | |
enemyAnimation.SetBool ("die", true); | |
alive = false; | |
StartCoroutine (Die ()); | |
StartCoroutine (FadeColor ()); | |
} | |
} | |
IEnumerator FadeColor () | |
{ | |
yield return new WaitForSeconds (1.5f); | |
rend.material.SetFloat ("_Mode", 2.0f); | |
rend.material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); | |
rend.material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); | |
rend.material.SetInt("_ZWrite", 0); | |
rend.material.EnableKeyword ("_ALPHABLEND_ON"); | |
startFade = true; | |
} | |
IEnumerator Die() | |
{ | |
yield return new WaitForSeconds (5); | |
Destroy (gameObject); | |
} | |
void CalculateRandomPos () | |
{ | |
NavMeshHit hit; | |
NavMesh.SamplePosition (transform.position + Random.insideUnitSphere * Random.Range (3, 10),out hit, 10, NavMesh.AllAreas); | |
ranPos = hit.position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment