Hello There!
I am currently trying to get the angle between my character and another object in space (on the x/z plane). Part of this, of course, requires me to know my character’s current rotation about his Y axis. I used transform.rotation.y, and multiplied that by PI to get the angle in radians. This almost works, but for some reason, it switches from - to + angle at an arbitrary point. On the 0-1 scale, that point seems to be ±.87
Why does it flip there instead of at 1? (or PI)?
If there is an easier way to do this (built in function or something) please let me know. Otherwise, please inform me of why this is happening, and what the fix is.
Thanks
You know that transform.rotation is a quaternion, right? You will probably want eulerAngles.
gahh… i got thrown off because I was using radians… And no, i didn’t know that Unity used Quats.
For anyone here in the future, this worked for me:
ang = body.rotation.eulerAngles.y * Mathf.PI / 180;
I too am trying to get the rotation of an object but not getting what I’d like to see. What I have is a large wheel with objects located on it as children. Each is placed every 30 degrees on the wheel. I can rotate the wheel using:
transform.Rotate(Vector3.up*30.0, Space.Self);
which works nicely. But…when I try to figure out at what angle the wheel is turned I cannot get anything that looks like degrees or something consistent. I’ll get 270 then 0.93838 then .58372 and other weird stuff. How is this done? I’m not that familiar with quaternions but know that I likely need to be using them but don’t know how.
What am I doing wrong?
It appears that I found a solution but have also discovered a potential other issue.
For my angle determination I’m using:
var ang = transform.rotation.eulerAngles.x;
print(ang);
This shows me each angle that is stepped through. A possible issue now is that there is creep in the rotation so for each time the wheel rotates the angle of each child is a little bit more, by fractions of a degree. I need to test now from spin to spin if it fixes itself or eventually I’ll end up with everything off far enough to break the functionality. Is there a way to better align and maintain alignment within the space?