How can i do a progesive rotation for a car . The rotation need to be on the vertical axis.
I know how to change the rotation but it does it too fast.I want that when the button in pressed start rotating slowly and when the button is released it need to stop rotating.
If you use the Input system of Unity you get this feature for free (if that’s what you want).
When you define an “axis” in the input manager you can specify the “sensitivity” and the “gravity”. Those two names might confuse a bit, but it’s really simple:
- The sensitivity defines how long it takes until the axis reaches full power (aka 1.0)
- The gravity defines how long it takes until the axis falls back to 0.0 after the button has been released.
The important thing is that a greater value means faster, a smaller value means slower. To calculate the time it takes until you reach max / min you just have to take the reciprocal of the value. A value of 1.0 equals a duration of 1 sec. That means a value of 2 will be 0.5 sec. A value of 4 will be 0.25 sec …
You can of course specify numbers smaller 1.0, so a value of 0.5 will be 2 sec.
In code you would use the axis value like this:
transform.Rotate(Vector3.up * speed * Time.deltaTime * Input.GetAxis("MyInputAxisName"));
Input.GetAxis can return a positive or negative value depending on if you have specified a button for positive / negative or both. By default you have the “Horizontal” axis which is already mapped to left / right and A / D
Also watch out for the “snap” setting. It will snap the value to 0 if you press the opposite direction. This might behave strange in a racing game, but sometimes it’s useful.