I have tried everything I can think of but the enemy will not rotate towards the player. I an using a capsule with a cube for a face, the face aligns with the Z axis. The enemy will follow me across the map but will never rotate. The enemy will traverse the map ok so I assume the navmesh is not the issue. I added a debug message to be sure the FaceTarget function was being called and it is many times. I even tried removing the code from the FaceTarget function and replacing it with LookAt(target) and even that would not work. I am assuming I screwed up in creating the enemy somehow but I have no idea how?
Here is my enemy AI script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyAI : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float chaseRange = 5f;
[SerializeField] float turnSpeed = 5f;
NavMeshAgent navMeshAgent;
float distanceToTarget = Mathf.Infinity;
bool isProvoked = false;
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
distanceToTarget = Vector3.Distance(target.position, transform.position);
if (isProvoked)
{
EngageTarget();
}
else if (distanceToTarget <= chaseRange)
{
isProvoked = true;
}
}
private void EngageTarget()
{
FaceTarget();
if (distanceToTarget >= navMeshAgent.stoppingDistance)
{
ChaseTarget();
}
if (distanceToTarget <= navMeshAgent.stoppingDistance)
{
AttackTarget();
}
}
private void ChaseTarget()
{
GetComponent<Animator>().SetBool("Attack", false);
GetComponent<Animator>().SetTrigger("Move");
navMeshAgent.SetDestination(target.position);
}
private void AttackTarget()
{
GetComponent<Animator>().SetBool("Attack", true);
}
private void FaceTarget()
{
Debug.Log("Facing " + target);
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * turnSpeed);
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, chaseRange);
}
}
The picture below shows all the components attached to the enemy
And this is the enemy