# Reletive rotation problems. (C#)

I’m trying to simulate a helicopter pitch/yaw/roll but I’ve run into a problem i cant find a solution for:

Demo:

http://tristanjc.com/unity/newweb.html

When you get into the air, the pitch and roll work fine from start rotation, but if you rotate yourself to face the camera (Z and C) and press pitch forward it tilts to the left and right instead. Aswell as other rotation bugs when rotating left or right.

The code for these bits are:

``````// ===== Yaw ======= //

if(Input.GetKey(KeyCode.Z)){
heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y + 0.01f, heli.transform.rotation.z, heli.transform.rotation.w);
}
}

if(Input.GetKey(KeyCode.C)){
heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y - 0.01f, heli.transform.rotation.z, heli.transform.rotation.w);
}
}
``````

– Roll

``````        float temproll = roll / 1.66666f;
temproll = (temproll - 30.0f) / 100.0f;
rollDegrees = temproll;
helirollSpeed = Mathf.Abs((temproll * 100.0f));

heli.transform.rotation = new Quaternion(heli.transform.rotation.x, heli.transform.rotation.y, rollDegrees, heli.transform.rotation.w);
``````

– pitch

``````        float tempPitch = pitch / 1.66666f;
tempPitch = (tempPitch - 30.0f) / 100.0f;
pitchDegrees = tempPitch;
heliSpeed = Mathf.Abs((tempPitch * 100.0f));

heli.transform.rotation = new Quaternion(pitchDegrees, heli.transform.rotation.y, heli.transform.rotation.z, heli.transform.rotation.w);
``````

How would I keep rotation locked for each one.

I would recommend using Transform.Rotate:

So you could have each control rotating around one specific axis (axis is relative to the direction your transform is facing), and shouldn’t affect the others.

For example, if you’d like to roll, then you’d rotate around Vector3.forward:

``````transform.Rotate(Vector3.forward * anglesPerSecond * Time.deltaTime);
``````

(for rolling left you’d use `-Vector3.forward` instead)

Using roll/pitch/yaw terminology from here: File:Rollpitchyawplain.png - Wikipedia

You want to roll left/right around heli.transform.forward

You want to pitch up/down around heli.transform.right

You want to yaw left/right around heli.transform.up

Try using angles and Transform.RotateAround and see if that helps.

You can also try Quaternion.AngleAxis for each rotation then multiply the quaternions before setting heli.transform.rotation.