# measuring rotation windup ?

Hi there,

Just having a little trouble trying to measure how many times a character flips over 360 degrees.
The rotation that unity shows in the transform is only 0 - 360 and doesn’t wind up, so a 720 flip will just report back as 360.

Does anyone know how I could measure this?
Thanks
Pete

There’s no way you can get this information directly from transform.rotation, since quaternions only record orientation, not how many times the object has spun around. You would need to keep track of the rotation angle of your object in a separate variable and add 360 whenever the value “wraps around”.

If you can give me a bit more information on what is going on in your game, I might be able to suggest some code to get you started.

Well there’s just a point where the character jumps and has to do a certain number of flips. The character’s base object has a Rigid Body component attached and I was going to use that object to measure the rotations.
The object is constrained to 2d space and can only spin on the x axis.

It was also important to know whether the character is doing forward or backward flips.

It’d be great if you have any ideas on how i could script this, I’m still having trouble.

Your situation might be different but I struggled with this a while back.

I was initially using modified code from here to try and check the rotation.

Then I realised (Duh) that I was actually rotating the object via key presses. So I just counted it at the source where I actually rotate the object manually rather than try to count it afterwards.

So if that doesn’t apply you can try the code in that link. I got it working for one axis ok, but had trouble modifying it for all axis (front flip, barrel roll etc).

Cool thanks!
I couldn’t really figure out that code but t gave me an idea (think this might have bee what they were doing there).

``````var deltaRotation : float = 0.0;
var currentRotation : float = 0.0;
var WindupRotation : float = 0.0;

function Update () {
deltaRotation = (currentRotation - transform.eulerAngles.z);
currentRotation = transform.eulerAngles.z;
if (deltaRotation >= 300) deltaRotation -= 360;
if (deltaRotation <= -300) deltaRotation += 360;
WindupRotation += (deltaRotation);
print (WindupRotation);
}
``````

It just measures the difference in rotation from the last frame and adds that to a windup total.

It has a few checks in there for when the rotation comes back to 360 from 0 (or the other way around)
Thanks Again!
Pete

Ahh Crap, I spoke too soon…
That works fin in my test scene but it apears that when my game character rotates its X,Y,Z axis aren’t as clean so sometimes the other axi’s flip to compensate…

Damn, not sure how I’m going to clean them up.
Don’t know why they are so messed up, the object has a configurable joint component and shouldn’t be able to move in any other direction.
Could it be an axis order thing? Guess not if it’s quaternion…

HI there,
Sorry to be a hassle but I was wondering if anyone knows a way to “clean” up my rotations.
The object looks fine in the viewport and I have constrained it to be able to only rotate on one axis but the X,Y,Z that unity gives me is all over the place, which is stopping my windup script from working

Any ideas would be great!
Thanks
Pete

If the axis you are rotating around is not one of the main X, Y or Z axes then you need to use another method to get the angle. The Quaternion class has a method called Angle that takes any two quats and returns the angle between them. You could use this to get the angle between any two consecutive rotations regardless of the direction of rotation.

Well i thought I was rotating around x but internally X axis is going mental, and the other axis keep flipping over to compensate. Is there any other way (aside from configurable joint) to make sure a rigid body doesn’t rotate on another axis?

The angle method is working okay by the way but it doesn’t know if the object is rotating forward or back or what angle it is really on so it’s not quite what I need.

Bloody rotations, why do they have to be so difficult.