Rotation Script

Hello,

I was wondering if there is a way to have an object spin and then eventually slow down until it stops.

thanks
kcarriedo

One way to do this is to place a rigid body component onto your object and attach a script that will apply a torque to it. It will spin and then slow down. How quickly is slows down will depend on a couple things, including the mass given to the rigid body, the physics material used, and the amount of the force applied.

The other way would be to not use the built-in physics stuff and to just simulate it with script. Such algorithms are easy to find on the web if you search for “kinematic behaviors” or “kinematics”.

For kinematics, you can get the orientation of your object like so:

float orientation = Mathf.Atan2(transform.position.x, transform.position.z)

Then you can alter the orientation value over time (in Update, for example), pseudocode: (this is just one of many ways to do this…just giving you one simple example)

rotationalAcceleration = some float value
rotationalDecay = some float value
perFrame:
    orientation += rotationalAcceleration
    transform.eulerAngles = TransformToVector(orientation);
    rotationalAcceleration -= rotationalDecay;
    if (rotationalAcceleration <= 0)
       stop

TransformToVector I think is this (pure memory recall, you’ll want to verify):

orientation.eulerAngles = new Vector3(Mathf.Sin(orientation), 0, Mathf.Cos(orientation));

Thank you so much razorcut this was a problem that I was having and to me it seem hard but when you explained I realized that its actually quite simple.

thanks again
kcarriedo

Shouldn’t you also multiply that by Time.deltaTime if its in an update?

Indeed.