- Home /
Range of Rotation Based off of Positions
I have two gameobjects, they both stay very close to their original positions but do move a little bit, I want one of them to have a range of eulerAngle values (only on the z axis). The range of eulerAngle values is supposed to be defined by a max and min relative to an angle that is directly aligned with the other object. The max can be the direct angle plus 20 and the min the direct angle plus 30, then I'll have the object keep rotating between these two values. Basically one object keeps another object in a view range I guess but rotates. How can I go about doing this? Specifically finding the direct angle, that's all I really need help with.
Answer by robertbu · Jun 16, 2013 at 05:54 AM
Since you are only rotating on the 'z' axis and want to keep another object in view, I'm assuming this is a 2D game. You can use Mathf.Atan2() to determine the absolute angle:
Vector3 v3T = watchee.transform.position - watcher.transform.position;
float angle = Mathf.Atan2(v3T.y, v3T.x);
The code works $$anonymous$$us the fact that the object looks directly downwards, but the rotation part works and everything it's just not looking at the object for some reason. It might be something specific to my code though so I have to look harder.
Note, this will align the right of the object with the angle. So you may need to add or subtract something from the angle calculation if some other side other than right is the looking side.
So what I had to end up doing is this:
var v3t : Vector3 = (target.transform.position - transform.position);
var rads = $$anonymous$$athf.Atan2(v3t.y, v3t.x)+($$anonymous$$athf.PI/2);
angle = RadDegConvert(rads);
And then add this function elsewhere in the code and it all worked, thanks!
function RadDegConvert(radians : float) : float {
return(radians*360/($$anonymous$$athf.PI*2));
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613104244im_/https://answers.unity.com/themes/thub/images/avi.jpg)