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.

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);