- Home /
add force to rotation
I want my object to rotate when I hit the "left" arrow key but when I release, I want it to keep rotating just a tiny bit instead of stopping all of a sudden. How do I do this?
Comment
Answer by Peter G · Aug 21, 2011 at 04:09 AM
Depending on how physically your object behaves, then you can either use rigidbody.AddTorque() which will cause your object to spin and then the physics engine will handle it slowing down, or you can add a damping effect your character motor:
private var lastInputValue : float;
function Update () {
var curInputValue = Mathf.Lerp(lastInputValue, Input.GetAxis("Horizontal") , Time.deltaTime);
//We move from the last input towards the current input gradually. The smaller the final parameter, the slower the object will dampen.
lastInputValue = curInputValue;
curInputValue *= rotateSpeed;
transform.Rotate( 0 , curInputValue * Time.deltaTime , 0 );
}