How to slow down the roation speed of a gameobject over time?

Updated on March 25, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
1 on March 24, 2019

Hello,

I am working on a hyper-casual app and right now I have a rotating circle game object. It rotates by clicking a button but when the button is released it slows down to a certain speed and stays there. I am wondering how would I go about when the button is released to have the game object slow down to a complete stop over time.

 

Here is how the code is set up right now.

 void Update()
 {
 if (_rotate)
 {
 transform.Rotate(0, 0, 75 * Time.deltaTime);
 }
 else if (!_rotate)
 {
 transform.Rotate(0, 0, 30 * Time.deltaTime);
 }
 }
  • Liked by
Reply
0 on March 25, 2019

You could have some variable called rotationSpeed that you multiply the rotation angle by, and then have this number gradually go down.

 

float rotationSpeed = 1f;
void Update()
{
Mathf.Clamp01(rotationSpeed); // This makes sure we can't go backwards, or too fast by clamping the value between 0, and +1
if (_rotate)
{
transform.Rotate(0, 0, 75 * Time.deltaTime * rotationSpeed);
rotationSpeed += Time.deltaTime; // This will increase the speed to 100% in about 1s
}
else if (!_rotate)
{
transform.Rotate(0, 0, 30 * Time.deltaTime * rotationSpeed);
rotationSpeed -= Time.deltaTime; // This will lower the speed to 0% in about 1s
}
}
  • Liked by
Reply
Cancel