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?