# simple plane code how to limit rotation

This is simple airplane code. I need to limit the z(yaw) and x(pitch) rotations. The following works (limits only z)-- like it runs without error, but doesn't work very well.

Is there a better way to do this?
Please code examples would be very very much appreciated. I promise that I will learn from them.

var moveSpeed : float = 50;
var yawSpeed : float = 90;
var pitchSpeed : float = 70;
//LEt's fly weeeee
function Update () {
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
transform.Rotate (-Vector3.forward * Input.GetAxis("Horizontal") * yawSpeed * Time.deltaTime);
transform.Rotate (Vector3.right * Input.GetAxis("Vertical") * pitchSpeed * Time.deltaTime);
}

//let's limit at least one angle so we can't do any crazy flying...
var MinClamp = -10;
var MaxClamp = 20;

function Start () {
var angles = transform.rotation.eulerAngles;
z= angles.z;
}

function LateUpdate(){

transform.rotation.eulerAngles = new Vector3(
transform.rotation.eulerAngles.x,
transform.rotation.eulerAngles.y,
Mathf.Clamp(transform.rotation.eulerAngles.z,MinClamp,MaxClamp));
}

You're on the right track, but what do you mean "doesn't work very well"? Could you please describe what you were expecting it to do and what it is not doing for you? Something like this should work to limit the axes:

var moveSpeed : float = 50;
var pitchSpeed : float = 70; //up/down tilting
var yawSpeed : float = 90; //left/right turning
//var rollSpeed : float = 90; //left/right rolling
var maxPitch : float = 40;
var minPitch : float = -20;
var maxYaw : float = 40;

private var eulers : Vector3 = Vector3.zero; //The current eulerAngles

function Start() {
eulers = transform.eulerAngles;
}

function Update() {
//Rotate
eulers.x += Input.GetAxis("Vertical") * pitchSpeed * Time.deltaTime;
eulers.x = Mathf.Clamp(eulers.x, minPitch, maxPitch);
eulers.z += Input.GetAxis("Horizontal") * yawSpeed * Time.deltaTime;
eulers.z = Mathf.Clamp(eulers.y, -maxYaw, maxYaw);
//eulers.y += Input.GetAxis("Lateral") * rollSpeed * Time.deltaTime;
transform.eulerAngles = eulers;

//Move
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
}

Why would you need to limit both yaw and pitch? This forces the plane to always move in the frustum defined by your limits and therefore always move in the same general direction - Is this what you intended?

Shame for me. I got yaw rotation mixed up with roll z rotation. So to clarify now rotation about: x=pitch y=yaw z=roll

Having said that, I made some minor changes to the code that skovacs1 posted (to enable a roll) and it's getting closer to simple plane code with limited pitch and roll (no limit on yaw). There are two primary problems left: a) how to convert the roll angles into left and right yaw rotation so that the plane can be steered left and right-- original code simply used pitch to do this but required the plane to be tilted 90 degrees to ground (yes this was an oversight in my first post) The effect I'm trying to achieve is to limit the roll and simply have the turning speed left or right be a factor of the roll angle.

b) and inverted roll controls-- ie. hitting right arrow key brings the right wing up (rolls left)-- needs to roll right.

I've attempted both and come up a bit short. Note the last line. It does yaw the plane, but in a very spastic way.

Any help appreciated. I want to avoid physics save for collision.

var moveSpeed : float = 50;

var maxRoll : float = 40;
var minRoll: float = -20;
var rollSpeed : float = 20;  //tilt left and right

var yawSpeed : float =60;//this is the turn left and right speed

var maxPitch : float = 40;
var minPitch : float = -20;
var pitchSpeed : float = 20; //up/down tilting

private var eulers : Vector3 = Vector3.zero; //The current eulerAngles

function Start() {
eulers = transform.eulerAngles;
}

function Update() {
//Rotate
eulers.x += Input.GetAxis("Vertical") * pitchSpeed * Time.deltaTime;
eulers.x = Mathf.Clamp(eulers.x, minPitch, maxPitch);
eulers.z += Input.GetAxis("Horizontal") * rollSpeed * Time.deltaTime;
eulers.z = Mathf.Clamp(eulers.z, minRoll, maxRoll);
//eulers.y += Input.GetAxis("Lateral") * rollSpeed * Time.deltaTime;
transform.eulerAngles = eulers;

transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
transform.Rotate ((-Vector3.forward) * Input.GetAxis("Horizontal") * rollSpeed * Time.deltaTime);

//doesn't work below:
//the idea is to turn left and right (Vector3.up)  by a factor of the tilt left and right rotation variable (eulers.z) given controls (i.e. ...GetAxis("Horizontal") and speed (i.e. yawSpeed)
transform.Rotate (Vector3.up*Input.GetAxis("Horizontal")* eulers.z* yawSpeed * Time.deltaTime); // this was an attempt to get left and right turning-- it jerks around and conflicts with previous line