# Smooth stop on a spinning object.

Hi, First off i’m very new to this, I have been through a few tutorials but I am very much a beginner so sorry if this is a noob question.

I’m experimenting with my own idea (very simple) I have a 2D button on screen and a 3D object (Cylinder) in the scene. When it plays the cylinder is spinning, when I press the 2D button I am sending a message to the cylinders script to stop spinning.

All of this works, but the stop I have is very abrupt. What I would like is for the cylinder to slow down to a stop when I press the button.
Here is my current script.

``````var spin = 5.0;
private var state = 0;

function ButtonPressed()
{
state++;
state = 1;
Debug.Log("1");
}

function LateUpdate ()
{
if (state == 1)
{
transform.Rotate(0.0,0.0,0.0);
}

else
{
state --;
if (state < 0)
state = 0;
transform.Rotate(0, Time.deltaTime +spin, 0);
Debug.Log("0");
}

}
``````

Try it like this…

``````var spin = 5.0;
private var state = false;

var drag = 0.9; //rate of "slow-down"
private var currentSpin = 0.0; //spin-speed

function Start()
{
currentSpin = spin;
}

function ButtonPressed()
{
//if the button is pressed, the
//state variable is flipped
state = true;
}

function LateUpdate ()
{
//if the button was pressed once...
if (state == true)
{
//...we multiply the spin-speed
//each frame (slowing it down)
currentSpin = currentSpin * drag;
}

//skip it, if the speed is smaller than 0.01
if(currentSpin > 0.01)
{
//rotate it with the currentSpin value
transform.Rotate(0, Time.deltaTime * currentSpin, 0);
}
}
``````

And you should multiply the Time.deltaTime with the currentSpin value, not add it (the smaller the deltaTime is, the smaller will be the rotation).

Hope that helps…

Fantastic, Yep got it working. (I did remove the var spin and just used the currentSpin). But it seems to work really fine. Thank you!