# How to make object lean in the direction it's going

I've got a space ship and I'm going for a starfox type look, so that when you move left or right it leans one way or the other. This is what I have to so far

``````    var speed : float = 20.0;
var rotatespeed : float = 10.0;
var target : Transform;

function Update () {
var controller : CharacterController = GetComponent(CharacterController);
//you can go up or down
transform.Translate(Input.GetAxis("Horizontal") *Time.deltaTime * speed, -Input.GetAxis("Vertical") * Time.deltaTime * 0, 0);

//always moving forward
transform.Translate(0,0,1 * Time.deltaTime);

//lean left right, up down
transform.Rotate(0, Input.GetAxis ("Horizontal") * 20*Time.deltaTime, 0);
transform.Rotate(Input.GetAxis ("Vertical") * 30 * Time.deltaTime, 0,0);

}

``````

I'm okay with it continually rotating left and right so they can turn I think, but if you hold up or down the ship just continues to rotate all the way around (lol) I was thinking about using an if statement to tell it to stop at a certain degree but Im not sure how to go about that, If(rotateamount = this much) stop rotating; ???

OK this is a script that I usually use to solve this

``````if(stop == false){
if(Input.GetKey(KeyCode.RightArrow)){
transform.Rotate(transform.up * Time.deltaTime * rotateSpeed, Space.World);
}
//you can add here the left
}
if(Input.GetKey(KeyCode.UpArrow)){
stop = true;
transform.Translate(transform.forward * Time.deltaTime * speed,Space.World);
}else{
if(Input.GetKeyUp(KeyCode.UpArrow)){
stop = false;
}
//You can add here the backawrd

``````

You might be able to use Mathf.Clamp to clamp the eulerAngles?

``````function Upate() {
transform.Rotate(0, Input.GetAxis ("Horizontal") * 20*Time.deltaTime, 0);
transform.eulerAngles.y = Mathf.Clamp(transform.eulerAngles.y,-30,30);
}

``````

you could also attach it to a configurable joint with min/max rotations. this might be easier to work with.