x


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

more ▼

asked Mar 04 '12 at 06:07 AM

avillago gravatar image

avillago
16 3 5 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
more ▼

answered Mar 04 '12 at 06:10 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

It worked!!! Thanks a lot!!!

Mar 04 '12 at 06:18 AM avillago
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x120
x109
x60

asked: Mar 04 '12 at 06:07 AM

Seen: 582 times

Last Updated: Mar 04 '12 at 07:12 AM