Can not get my enemy to rotate towards the player (Solved)

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

5676928--591955--upload_2020-4-6_11-14-0.png

Hi @JohnnyMc

Have you tried disabling navmesh agent’s control over rotation?

1 Like

That page you linked is just about empty but I found the code I think… I tried placing this line:

navMeshAgent.updateRotation = false;

in start but no luck.

Maybe because your y rotation is always set at 0.

@JohnnyMc

“That page you linked is just about empty but I found the code I think”

There isn’t any more documentation available, but I think it is better than nothing.

Your FaceTarget method will look just fine. I’ve just a couple of days ago used very similar way to rotate/look at targets for navmesh agents. I didn’t properly check your other code. It might be something else.

I figured it out!
I had locked the rotation in an animation. I was trying different things and when I turned off the animator component everything worked great.
Thanks for all the suggestions and especially for your help eses. That navmesh clue helped me to think it might be something else besides the code that was overriding the rotation.

1 Like