AIController with Movement

Hey guys,

I just wanted to present my code. Maybe its not the best and we are going into another direction but I worked it out without help and it worked pretty well. I was annoyed by the enemy trying to run “into” me and pushing me away so I also set a minimum distance. A big thanks to the guys from the tutorials and keep the good work up :slight_smile:

namespace RPG.Control {
    public class AIController : MonoBehaviour
    {
        [SerializeField] float chaseDistance = 5f;
        [SerializeField] float minimumRange = 1f;

        private void Update() {
            
            GameObject player = GameObject.FindGameObjectWithTag("Player");
            float playerDistance = Vector3.Distance(transform.position, player.transform.position);

            if (playerDistance < chaseDistance && playerDistance > minimumRange)
            {
                ChasePlayer(player);
            }
        }

        private void ChasePlayer(GameObject player)
        {
            transform.LookAt(player.transform.position);
            GetComponent<NavMeshAgent>().destination = player.transform.position;
        }
    }
}
1 Like