Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created October 28, 2017 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanYin/4d7eadace164ae251ef7032134c496eb to your computer and use it in GitHub Desktop.
Save JonathanYin/4d7eadace164ae251ef7032134c496eb to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class EnemyMovement : MonoBehaviour
{
Transform player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
UnityEngine.AI.NavMeshAgent nav;
public float speed = 6f;
void Start ()
{
nav.speed = speed;
}
void Awake ()
{
player = GameObject.FindGameObjectWithTag ("Player").transform;
playerHealth = player.GetComponent <PlayerHealth> ();
enemyHealth = GetComponent <EnemyHealth> ();
nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
}
void Update ()
{
if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
{
nav.SetDestination (player.position);
}
else
{
nav.enabled = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment