The Strangest AI Error - They look the wrong way

Hey, I’m trying to get some AI to work as it should, and my problem is this;

When they enter a trigger collider attached to their head if it is an enemy, they’ll notice them (working)

Then, they should look directly at the player (not working.)

I’ve done a few things to debug this, and I’ve come to some interesting conclusions;

They do not look directly at the player, and albeit it should be precise and all that it isn’t.

I’m going to post some of my code

void Face_Task()
    {
     
        if ( task_obj == null )
        {
            return;
        }
     
        Vector2 toForward = Target_Direction();
     
        transform.forward = new Vector3(toForward.x, 0, toForward.y);
        return;
    }

    bool Facing_TObj()
    {
        if ( task_obj == null )
        {
            return false;
        }

        Vector3 targeting_direction = Vector3.Normalize (transform.position - task_obj.transform.position);

        if ( Mathf.Abs (transform.forward.z - targeting_direction.z) < .1F && Mathf.Abs (transform.forward.x - targeting_direction.x) < .1F ) // Not sure if this is the best.
        {
            return true;
        }
        else
        {
            return false;
        }
    }
 
 

    void Aiming_Funcs()
    {
        if ( task_obj == null )
        {
            Current_Task = Task.None;
            return;
        }
        else
        {
            Face_Task();

            if ( Facing_TObj() )
            {

                if ( dispersion < .3F )
                {
                    // For actual firing.

                    Face_Task();
                    return;
                }
            }
            else
            {

            }


            return;
        }
    }

Also, it does not pass Facing_TObj() so nothing below it gets called, so I also know by that it actually isn’t facing the technical forward. And GameObject task_obj is the player, which I know is assigned right. It would seem to be that the player object is not centered properly, and granted that could be it but even so, it isn’t that off center. So I can’t imagine it’s that much of the problem.

After looking closer and testing it in-game, it also appear they look at the person’s right on the x or z axis.

Is your model facing the positive Z axis in whatever 3d modeling program it was made in? I believe that will affect its forward axis in the unity engine which could be causing your problem… also, this doesn’t look right to me :

transform.forward = new Vector3(toForward.x, 0, toForward.y);

Shouldn’t you be passing in the toFoward.z instead of y? I’m not sure you should even be changing the forward axis on transform for rotation purposes, maybe you should try to use transform.LookAt() instead : Unity - Scripting API: Transform.LookAt.

I tried using transform.LookAt() and got the same results. It may be a problem with model being imported wrong, I’ll look into that.

Again though it may not be, because again regardless of them looking the wrong way they were set to the technical value for the forward to face it’s task_obj, their real forward was not on center. So yeah, it might not be. I’m sorry, I have a hard time explaining things sometimes.