Unity 3D AI enemies with wrong rotation

2020-02-14 c# object unity3d rotation

I have a problem with rotation of enemies in Unity. I used this script:

Transform target;
NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
    target = PlayerManager.instance.player.transform;
    agent = GetComponent<NavMeshAgent>();
    FaceTarget();
}

// Update is called once per frame
void Update()
{
    float distance = Vector3.Distance(target.position, transform.position);
    agent.SetDestination(target.position);
    FaceTarget();
}
void FaceTarget()
{
    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 * 10f);
}

But if I put an enemy on scene and started game they were rotated by 90 degrees. Its my first game and I dont know ho to fix it. Is it a script fault? Thanks in advance.

Answers

Related