- Home /
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)){
if(engineReady){
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)){
if(engineReady){
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.
Answer by Tomer-Barkan · Oct 10, 2013 at 08:13 AM
I would recommend using Transform.Rotate:
http://docs.unity3d.com/Documentation/ScriptReference/Transform.Rotate.html
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)
Thanks this worked fine, but how would i return the value for pitch if its on .forward, Would it still just be "heli.transform.rotation.x".
pitch would be rotating around the X axis, so you'd use transform.Rotate(Vector3.right * anglesPerSecond * Time.deltaTime);
and yaw would be rotating around the Y axis, so you'd use transform.Rotate(Vector3.up * anglesPerSecond * Time.deltaTime);
Answer by $$anonymous$$ · Oct 10, 2013 at 08:38 AM
Using roll/pitch/yaw terminology from here: http://en.wikipedia.org/wiki/File:Rollpitchyawplain.png
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.
Your answer
Follow this Question
Related Questions
Objects not rotating right half the time based on another object 0 Answers
Flip over an object (smooth transition) 3 Answers
transform.Rotate will not work 1 Answer
Why is this rotation acting odd? 0 Answers