Finding the Angle for Enemy Ai / Stealth Tutorial Script Ch.4 lesson 5

  float FindAngle(Vector3 fromVector, Vector3 toVector, Vector3 upVector)  // float function
     {
       if(toVector == Vector3.zero) //if desired velocity is zero then return zero // to avoid error
         return 0f;
       float angle = Vector3.Angle (fromVector, toVector);
       Vector3 normal = Vector3.Cross(fromVector, toVector); //finds cross product
       angle *= Mathf.Sign(Vector3.Dot (normal,upVector));  //if normal and up vector are pointing in same direction the result is greater than zero(angle is to the right)
       angle *= Mathf.Deg2Rad;  //if the normal however is pointing down in relation to the upVector, then the result will be less than zero (and the angle will be to the right)
        //if the normal and upVector were perpendicular to each other than that would be zero

       return angle;
     }

I was trying to find the angle of the AI for a script, and I just want to know if my knowledge is correct. If you find the cross product/normal of the angle (the forward vector and the vector pointing to the player), if the normal is Not perpendicular to the Up Vector, and is pointing in the same direction as that Up Vector (or is at least above zero) then the angle would be to the right. If the normal is pointing in the opposite direction of the Up Vector then the angle would be to the left. Is this correct am I missing something, or is this just completely wrong?

Think I figured it out .If the player is to the right of the enemy, then the normal will face the the same direction as Vector.Up and the angle will be to the right. Vice versa and the angle will be to the left.

I think, if I’m not mistaken, that if only calculate normal between fromVector/toVector and upvector not apply perspective.

To calculate the angle from the perspective (upVector), it is necessary to recalculate normal vector(cross product) between the normal obtained and up vector.

using UnityEngine;
using System.Collections;

public class temp : MonoBehaviour {

    public float angle;
    public Vector3 UpView = Vector3.up;
    public Transform A;
    public Transform B;

    float SignedPerspectiveAngle(Vector3 fromVector, Vector3 toVector, Vector3 upVector)
    {
        fromVector = Vector3.Cross(Vector3.Cross(fromVector,upVector),upVector);
        toVector   = Vector3.Cross(Vector3.Cross(toVector  ,upVector),upVector);
        return Vector3.Angle(fromVector,toVector)*Mathf.Sign(Vector3.Dot(Vector3.Cross(fromVector,toVector),upVector));
    }

    void Update(){

        angle = SignedPerspectiveAngle(A.position,B.position,UpView);

    }

}

I’ll take a look at it. Though i’m not 100 percent with you when you say perspective. I’ll definitely keep it in mind though when I finish this tutorial.