Before i’ll blow up my computer, i want to know how to rotate object (i’m using Horizontal axis button), i mean what function to use so i can restrict it to 30 degrees both ways (-30 & 30). What it’s for? Ship which during turning L/R tilts in this direction so it looks better and after HorRotation(axis-button) is up it returns to Z=0 (smothly).
No matter what i do or what i’ll find in internet… it only works worse. (yes, i tried Mathf.Clamp… but i used it wrong as u can see)
BIG THANKS in advance!
To tilt to the side you’re turning, use the Horizontal axis to control directly the localEulerAngles.z angle - this way the ship will return to zero when the control is released. But there’s a problem: the tilt and turn rotations interfere with each other. To avoid this problem, you must have two objects: create an empty object - let’s call it “Ship” - then child your ship model to it - let’s call it “Body”. Use Ship to turn left and right, and Body to tilt to the side you’re going. The hierarchy should be:
Ship <- this object moves and turns left/right
Body <- this object contains the model, and is used to bank left/right
Your script would be something like this (attached to Ship):
var hSpeed: float = 2; // left/right banking speed var hAngle: float = 15; // left/right banking angle var turnSpeed: float = 60; // turn speed in degrees per second private var hBank: float = 0; private var body: Transform; function Update(){ if (!body) body = transform.Find("Body"); // find the Body object var h = Input.GetAxis("Horizontal"); // read the control transform.Rotate(0, h * sensitivity * Time.deltaTime, 0); // rotate left/right hBank = Mathf.Lerp(hBank, h, Time.deltaTime*hSpeed); // smooth control with Lerp var angles = body.localEulerAngles; // incline body by hAngle degrees left/right angles.z = -hBank*hAngle; // bank left/right body.localEulerAngles = angles; }