- Home /
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; ???
Answer by Uriel_96 · Nov 22, 2010 at 03:13 AM
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
Ohhh, I see, that script is for increasing forward speed! I thought it was for rotate! I just altered it to rotate. That's what I was asking about is having the ship tilt a little in the direction it's going. That's a nice increase speed script though :)
Answer by Design3.com · Dec 16, 2010 at 03:58 AM
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);
}
Answer by psychentist · Apr 20, 2011 at 05:29 AM
you could also attach it to a configurable joint with min/max rotations. this might be easier to work with.
Your answer
Follow this Question
Related Questions
How do I make gameObject.transform.rotation.z equal to a set float value? 2 Answers
Unity 8-Directional Locked Movement (Super Mario 3D World Style) 3 Answers
how to rotate GameObject towards joystick 1 Answer
Rotating wrong 0 Answers
Rotate towards target with limitation on certain axis? c# 2 Answers