- Home /
Question by
rsamrry · Jun 20, 2014 at 04:12 PM ·
rotationquaternionsflyingeuler angles
How can I control the yaw, pitch and roll of a flying object?
Hi, I want to make an object fly through space and control its yaw, pitch and roll through buttons on the screen and with accelerometer input. At first I thought euler angles will do the trick as they describe precisely the yaw, pitch and roll of an object but I was wrong. I am guessing this can be achieved through quaternions but I don't know anything about them. Please help me figure out what changes I need to make to the code below in order to achieve my aim. In the code below, the y-axis represents yaw, the z-axis roll, and the x-axis pitch. Thanks.
void LateUpdate () {
if (btnsAction.leftPressed && !btnsAction.rightPressed) {
eulerY -= 10f*Time.deltaTime;
}
if (!btnsAction.leftPressed && btnsAction.rightPressed) {
eulerY += 10f*Time.deltaTime;
}
if (btnsAction.upPressed && !btnsAction.downPressed) {
eulerX -= 10f*Time.deltaTime;
}
if (!btnsAction.upPressed && btnsAction.downPressed) {
eulerX += 10f*Time.deltaTime;
}
eulerZ -= 10f*Input.acceleration.x*Time.deltaTime;
rotation = Quaternion.identity;
rotation.eulerAngles = new Vector3(eulerX, eulerY, eulerZ);
transform.rotation = rotation;
}
Comment
As long as you don't read from eulerAngles, they should work. Note line 15 and line 18 should be deleted. They end up resetting the rotation to 0.0.
Your answer
![](https://koobas.hobune.stream/wayback/20220613152033im_/https://answers.unity.com/themes/thub/images/avi.jpg)