So I’m trying to figure out how to get a value for how fast an object is spinning. I’m doing the rotation outside of the physics engine and I’m controlling the object with multiple scripts so I really need to derive it from the z rotation value.
And for some reason this has got me plain stuck.
Here’s my first go at it:
public float spin;
public float lastZ;
// Update is called once per frame
void Update () {
spin = transform.rotation.z - lastZ;
lastZ = transform.rotation.z;
}
So one obvious issue is that when the object makes a revolution it resets the z value to zero.
But I’m also noticing that the value is going up and down in a sign wave.
It’s probably going up and down in a sign wave because you’re comparing only one of three euler angles.
But don’t just trying adding up the difference of all three or something, you’ll have the same problem with wrapping around only with three variables.
This Quaternion function could help with calculations:
This takes two rotations and finds the angle between them, and neatly avoids the problem of when you wrap around from 0 to 360.
So instead of storing lastZ, you can store whole lastRotation as a Quaternion and compare it to the new rotation using Angle().
I can help you if you tell me exactly what you want. Your question is kind of convoluted.
And you’re Z is sinusoidal because you’re measuring the Z value of a circle. That his how a sin wave is defined.
I know math and physics, but quaten…whatever the hell that is is confusing the situation.
What do you know? The object’s dimensions?
What do you want to know?
Without knowing more…your solution is going to involve a summation of the z derivatives: Print out the derivative and you’ll see it approahing 0 over and over again: This is your max and min values. The number of times it approaches 0 divided by 2 are your number of revolutions.
I’m wanting to figure out the degree of change on the z rotation between fixed updates.
I want to take this value so that I can apply it to the my sprites so that the they turn into the change of the rotation.
EDIT: I know this sound like putting the cart before the horse, but my intention is to have the animation independent of the script that’s turning the object since I’m looking to make a several different scripts that change the object’s rotation in their own way.
You’re shooting yourself in the foot. You’re asking for help before you define your situation or your problem. Sit down, write out everything you know about your system, find out exactly what you want to know, and then come back and ask a clear and specific question. You’re going to get convoluted responses until you do that.