- Home /
pitch yaw roll user input on an object?
How would I implement (in c#) a pitch yaw roll input controller on a flying gameobject?
I'm looking at past posts but not getting a clear idea.
I want the user to input from a keyboard
- a or d for positive/negative yaw
- w or s for positive/negative pitch
- q or e for positive/negative roll
The control should work as if sitting in the cockpit of the game object.
Does Unity offer a script to do this?
If not, could someone provide c# code to do this? I know how to get the user input. Need the code that applies the change to the gameobjects orientation.
if (Input.GetKey(KeyCode.A)) "what goes here?" ; //yaw left
if (Input.GetKey(KeyCode.D)) "what goes here?" ; //yaw right
if (Input.GetKey(KeyCode.W)) "what goes here?" ; //pitch up
if (Input.GetKey(KeyCode.S)) "what goes here?" ; //pitch down
if (Input.GetKey(KeyCode.Q)) "what goes here?" ; //roll counterclockwise
if (Input.GetKey(KeyCode.E)) "what goes here?" ; //roll clockwise
Appreciate any help as 3d geometry is not my strong suite. Thanks!
Answer by rsud · Jan 31, 2013 at 04:57 AM
http://keithmaggio.wordpress.com/2011/07/01/unity-3d-code-snippet-flight-script/
This link provided by alucardj above has some simple code that works great!
Thanks alucardj !!
Answer by robertbu · Jan 21, 2013 at 02:49 AM
In the couple of instances I've had to deal with kind of problem, my solution was to construct a gimble out of game objects. Note I was not using the physics engine, so I don't know how well this solution will translate to your flying object.
Create three empty game object and place then at the origin along with your flying object. By draggin and dropping in the hierarchy view, make all three children of each other. I don't think order matters, but when you get done you should have something like Yaw at the top, Pitch as the child of Yaw, Roll as a child of Pitch, and your flying object as the child of Roll. Next attached the following, short script to the Pitch, Yaw, and Roll game objects.
using UnityEngine;
using System.Collections;
public class RotateByKey : MonoBehaviour {
public KeyCode kcNegative = KeyCode.A;
public KeyCode kcPositive = KeyCode.D;
public Vector3 v3Rotation = new Vector3(0.0f, 1.0f, 0.0f);
void Update () {
if (Input.GetKey(kcPositive))
transform.Rotate (v3Rotation);
if (Input.GetKey (kcNegative))
transform.Rotate (-v3Rotation);
}
}
In the inspector, set the values for kcNegative (the keycode for negative rotation), kcPositive (the keycode for positive rotation), and v3Rotation (how the game object should rotate). Each empty game object should rotate about only one axis. So Yaw get (0,1,0), Pitch gets (1,0,0) and Roll gets (0,0,1). Note a larger value such as (3,0,0) will make the turn faster on the specified axis.
This was exactly what I was looking for and this approach helped me to get a gimbal model working correctly. Even though your answer was not voted the "Best" answer for this particular question, it was for me! -many thanks for posting this Robert.