- Home /
how to gradually slow down the speed of the wheel?
I'm new to Unity and trying to make a spin wheel game which works by spinning when i click and gradually slows down with time and the pointer points towards a specific segment referring to the prize.
I've a few ways to do this, like creating each segment of the circle as a rigid body and adding a fixed joint to the adjacent segment (10 in total). and putting a hinge joint in the center of a circle but it doesn't works.
next i tried something with transform.Rotate
something like this.
function Update() {
if (Input.GetMouseButton(0)) {
var tran: float;
var i = 0;
tran = Time.deltaTime * 10;
transform.Rotate(0, 0, -tran);
}
}
it didn't worked either as the wheel only spins when i click on it, so, slowing down is not a case here too.
i saw this answer most closely related but still no help.
is it something to do with Animation? any guesses how can i achieve this?
Answer by Captain_Pineapple · May 22, 2018 at 04:49 PM
Hey there,
the code you provided is almost correct, you only have to pack the code a bit different:
function Start(){
tran = 0;
}
function Update() {
if (Input.GetMouseButton(0)) {
if(tran < 0.1f) // makes sure to only accellerate if the wheel is below a certain speed;
{
tran = 100.0f;
}
}
//reduce speed "tran" by 5% every frame:
tran = 0.95f * tran;
// rotate wheel every frame by given speed "tran"
transform.Rotate(0, 0, -tran*Time.deltaTime);
}
now you only have to add your variable tran as a float to your class and you should be good to go. This code should now: Accellerate your Wheel to a value, but only if its speed is below 0.1f and slowly decellerate. Please bear with me if the code is not correct in syntax, i only write in C# so javascript is really not my territory.
hi, thank you so much for the reply, and yea i guess i was pretty close, however , the wheel stops pretty soon, maybe because of the constrains , maybe lowering down the multiplier might work but do you think there is a better way to achieve a bit more realistic slowing down?
I read somewhere that we can provide torque, do you think that would be good idea? thanks again!
well objviously the first choice would be to take the 0.95f
and change it to some 0.99f
or something similar. The closer it gets to 1, the slower it will slow down. Obviously you could also take some linear approach.
Lets say you want it to get slower by 10 units per second. change the calculation to tran = tran - (10*Time.deltaTime)
. So your wheel should be at a full stop after around 10 seconds. (At this point you should include a check so that your "tran" does not fall below 0 ;))
I just personally don't like such a linear approach but perhaps it is the right thing for you :)