- Home /
I rotated an object around another object's y axis. How do I find the object's degrees in rotation based on that same axis?
I rotated object A around B's Y axis using Transform.Rotate(). Sadly, I know of no way to get the degrees of y rotation for this after the fact. There is a way to get the local rotation and a global rotation, but I know of no way to get A's degrees of rotation around B's y axis. Any of you know how to do this?
Answer by andrew-lukasik · Mar 17, 2021 at 12:23 AM
From what I can understand you probably asked for this:
// A's degrees of rotation around B's y axis:
float degrees = Vector3.Angle( Vector3.ProjectOnPlane(A.forward,B.up).normalized , B.forward );
Tester gizmo:
[SerializeField] Transform A = null;
[SerializeField] Transform B = null;
void OnDrawGizmos ()
{
Gizmos.color = Color.yellow; Gizmos.DrawRay( A.position , A.forward ); Gizmos.DrawRay( B.position , B.forward );
Gizmos.color = Color.green; Gizmos.DrawRay( B.position , B.up );
UnityEditor.Handles.color = Color.green;
UnityEditor.Handles.CircleHandleCap( 0 , B.position , Quaternion.LookRotation(B.up,B.forward) , 1f , EventType.Repaint );
// A's degrees of rotation around B's y axis:
Vector3 A_forward_projected = Vector3.ProjectOnPlane( A.forward , B.up );
float degrees = Vector3.Angle( A_forward_projected.normalized , B.forward );
Gizmos.color = Color.green; Gizmos.DrawRay( B.position , A_forward_projected.normalized );
Gizmos.color = Color.red; Gizmos.DrawRay( B.position , A_forward_projected );
UnityEditor.Handles.Label( transform.position , $"angle: {degrees}" );
}
Answer by Zuminez · Mar 17, 2021 at 12:12 AM
Transform.Rotate(Vector3 axisToRotateAround, float degreesOfRotation, Space Space.Self);
Eventually it dawned on me that I could just make a global variable....
float yRotation;
And add to it whatever I put in as the degreesOfRotation...
yRotation += angleToTurn * directionToTurn * turnSpeed * Time.deltaTime; A.transform.Rotate(b.transform.up, angleToTurn * directionToTurn * turnSpeed * Time.deltaTime, Space.World);
And I no longer need a Unity function that will give that to me. I am still curious as to how it's done though.
Your answer
![](https://koobas.hobune.stream/wayback/20220613030816im_/https://answers.unity.com/themes/thub/images/avi.jpg)