Roll function for verhicle

Hi all

I hope someone can help me out with this. Because i cannot figure out howto get this problem solved.

I have a game with a vehicle, when pressed right key i want the vehicle to roll over to the right side. When pushed left i want it to roll over to the left side.

When the key is not pressed. The verhicle should automatic return to the straight position.

I already have the part where i check which key is pressed and i know how to undertake actions but i can’t figure out how to make it automaticly return to the straight position.

Does anyone have any idea howto do this? Should i do this by measuring time and every second/milisecond move the rotation back or something?

If you are using the “Horizontal” axis to control the vehicle, do the following:

var rollSpeed: float = 2;
var rollAngle: float = 15;
private var roll: float = 0;

function Update(){
    var dir = Input.GetAxis("Horizontal");
    roll = Mathf.Lerp(roll, dir, Time.deltaTime * rollSpeed);
    transform.eulerAngles.z = -roll*rollAngle;  // roll

If you are using GetKeyDown instead of GetAxis, use this to set dir:

function Update(){
    var dir = 0;
    if (Input.GetKeyDown("roll-left-key")) dir = -1;
    if (Input.GetKeyDown("roll-right-key")) dir = 1;
    roll = Mathf.Lerp(...

Anyway, you may have problems if your vehicle turns to left and right, because the roll will tilt the Y angle, giving weird results. If this is the case, you could child the vehicle model to an empty object, and use this object to turn and move, while the vehicle model will only do the rolling - take a look at this question to know how to do this.