Get angle relative to GameObject

I have two GameObject’s that are rotated at a arbitrary angle and in a script attached to one of the GameObject’s I would like to know the angle to the other GameObject relative to its own rotation. In the following illustration I have the script attached to the lower object and I want to know the “red” angle. In this illustration they are both pointing up, but that will not always be the case as they both rotate at will. I have tried many things now but it just does not work for me, but I am sure it is easy? Any ideas on how to do this?

28262-anglerelative.png

1 Answer

1

That was actually easy… I simply did this (transform.up is my forward):

Vector2 toVector = target.transform.position - transform.position;
float angleToTarget = Vector2.Angle(transform.up, toVector);

Niceeeeeee