using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; public class EnemyMovement : MonoBehaviour{ // variables // public vars // these should be set in the editor for each enemy however the designers want // distance from player before the enemy starts running public float noticeDistance; // speed for when the enemy is out of range but it will still be making its way towards the player public float creepSpeed; // speed for when the enemy is chasing the player public float chaseSpeed; // private vars private GameObject player; private NavMeshAgent navMesh; private Transform child; // Start is called before the first frame update void Awake(){ // this is the player player = GameObject.FindGameObjectWithTag("Player"); // this is the built in AI script navMesh = transform.GetComponent (); // this is the visuals of the enemy child = transform.GetComponentInChildren(); } // Update is called once per frame void Update(){ // check if the player is within the notice range if (Vector3.Distance (transform.position, player.transform.position) <= noticeDistance) { // make the enemy chase the player navMesh.speed = chaseSpeed; } else { // make the enemy slowly creep up on the player (or set creepSpeed to 0 to make the enemy not move at all) navMesh.speed = creepSpeed; } // this will make the enemy face the player child.LookAt(player.transform); // rotate the y by 90 degrees and set the x and z to 0 child.rotation = Quaternion.Euler(0, child.rotation.eulerAngles.y -90, 0); // tells the AI to follow the player navMesh.SetDestination (player.transform.position); } }