- Home /
Kinect controlled flight issue
Hello, I'm creating a game that uses Kinect to control flight, but I am having really big problems. The speed is constant, and the camera is attached to the head joint, so the idea is wherever the player turns the head, is where the player should turn and look at and continue moving forward. Right now instead of turning, the character it's only changing the forward vector's direction, and it's making my character fly sideways or even backwards. Any ideas on how can I achieve that???? Here's my code:
Vector3 newDirection = Vector3.zero;
newDirection = new Vector3(GameObject.Find("MainCamera").transform.forward.x, 0, GameObject.Find("MainCamera").transform.forward.z);
if (newDirection != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(newDirection);
transform.forward = newDirection;
}
transform.Translate(transform.forward * (movementSpeed / 2) * Time.deltaTime);
Any help is appreciated
Answer by aldonaletto · Mar 04, 2012 at 06:10 AM
The problem here is the Translate direction: Translate by default uses local coordinates, thus you can simply use Vector3.forward to make it go in its forward direction.
But you could do the whole job in a much simpler way: get the main camera's forward direction in newDirection, zero the y component and assign it directly to the character's transform.forward vector: this will rotate the character to the main camera's forward direction - but only in the horizontal plane:
Vector3 newDirection = Camera.main.transform.forward; newDirection.y = 0; // force direction to the horizontal plane only transform.forward = newDirection; // turn the character to newDirection // move in the character's local forward direction: transform.Translate(Vector3.forward * (movementSpeed / 2) * Time.deltaTime);