I am thinking about this for a long time, and personally i am bad at maths and all this. Okay firstly i wanted to script something like that but i do not know how to implement it in script form. So hoping someone can help me. Thanks.
Imagine this as a top down view. (-O- refers to objects in game)
-O- Object 2
-O-
Object 1
Basically i need to calculate how much object one need to turn in order to see object 2. Sorry if this is too confusing.
first, you create a line in the direction that object1 is facing, then get the angle between that and object2. Next, use a dot of the vector to see if it is on the left or right to get positive and negative.
var object1 : Transform;
var object2 : Transform;
// get the forward direction of object1
var forward=object1.forward;
forward.y=0; // this removes the 3d angle, so it is in 2d.
// get the direction of object2 from object1
var toObject=object2.position - object1.position;
toObject.y=0; // this removes the 3d angle, so it is in 2d.
// get the angle
var angle=Vector2.Angle(forward, toObject); This returns the positive angle of object1 to object2
// get the right direction of object1
var right=object1.forward;
right.y=0; // this removes the 3d angle, so it is in 2d.
// tell if the angle is left or right
var dot=Vector3.Dot(right, toObject);
if(dot < 0) angle=-angle; // if we are not right, then we are a negative angle.