- Home /
Rotation from another object
I am trying to get one object's rotation relative to another object, in euler angles as a degree value between (0 to 180) or (0 to -180). Ultimately just need the Vector3.y axis.
If the camera was looking directly towards the object it would give a 0 deg, and if you were looking directly away in the opposite direction it would give 180.
I am not trying to rotate any of the objects, just need to log this value.
Any help would be appreciated, thanks!
Answer by phosphoer · Feb 19, 2020 at 06:02 PM
Since you only care about the angle on a single axis, I'd recommend using Vector3.SignedAngle
.
// Get Y angle between A and B
float yAngle = Vector3.SignedAngle(vecA, vecB, Vector3.up)
The above would give us the angle difference between those two vectors, so we can change it slightly to find whether A is looking at B. You'd just substitute in your camera's transform here for transformA and the object in question for transformB.
// Get Y angle between A's forward and the vector going from A to B
Vector3 aToB = transformB.position - transformA.position;
float yAngle = Vector3.SignedAngle(transformA.forward, aToB, Vector3.up)
Your answer
![](https://koobas.hobune.stream/wayback/20220612223639im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Quick Angles Question 2 Answers
Vector3.SignedAngle wrong direction when crossing the 0 point 1 Answer
How Do I accurately rotate a player 180 degrees about the Z axis? 1 Answer
Change a property based on angle? 1 Answer
How to calculate the rotation of a hexagon based on its six vertices position 2 Answers