Finding an object's spin velocity

Hey everyone,

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.

Does anyone have a suggestion on how to do this?

Thanks for any help you could give me!

you probably want to use euler angles, and it will probably still not work due to gimbal lock.

does the item have a rigidbody?

No. I’m avoiding unity physics to save on CPU.

I’ll try euler though and let you know if that does the trick.

Thank you for the suggestion!

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().

do you have colliders?

Sorry for the long pause between replies.

Only as triggers.

Getting closer…

    public float spin;
    public Quaternion lastRotation;

    void Start () {
        lastRotation = transform.rotation;
    }

    // Update is called once per frame
    void Update () {
        spin = Quaternion.Angle(transform.rotation, lastRotation);
        lastRotation = transform.rotation;
    }
}

It now has the angle right, but the spin value jumps to zero when it crosses the 0/360 point…

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.

  1. What do you know? The object’s dimensions?
  2. 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.

Revolutions/deltatime = revolutions/second

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.

Thank you for help on this. :slight_smile:

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.

?

When what crosses the 0/360 point?

How are you rotating this thing? Maybe you can get directly from there how fast it’s rotating.

I’ve pretty much given up and decided to write the spin into all of the other scripts.

And the rotation goes to 0 when it makes a full revolution.