0
votes

I'm working on a new control scheme for the Oculus Rift that utilizes the tilt of the headset to move the player. Meaning, you tilt your head back and forth to go forwards and backwards, and from side to side to strafe. The camera is tied to a rolling sphere, as that gave it a nice sense of acceleration, and feels a bit like kind of flying around. So far, it's working quite well, but only on the global axis. So if you turn to the left and tilt your head forward, you still go forward according to the starting position (Which if you're facing to the left means you'll go right). I'm trying to fix it so that you can go forward and strafe relative to the direction the camera is facing, but with no luck. I have a strong sense that it's something ridiculously simple, but I just can't seem to find it. Any help is very much appreciated!

Here is what I have on the rolling sphere right now:

public GameObject RightCamera;


void FixedUpdate(){
    float angleX = RightCamera.transform.eulerAngles.x;
    angleX = (angleX > 180) ? angleX - 360 : angleX;

    float angleZ = RightCamera.transform.eulerAngles.z;
    angleZ = (angleZ > 180) ? angleZ - 360 : angleZ;

    Vector3 movement = new Vector3 (-angleZ, 0, angleX);

    GetComponent<Rigidbody>().AddForce (movement);

}
2
I'm guessing you are just applying this movement directly to some kind of a player? you need to adjust a forward vector by this insteadSayse
I'm applying it to a rolling sphere because I liked the way it felt rolling around. Which means I can't really use the local rotation of the sphere because, as spheres go, it's rolling.syverlauritz

2 Answers

0
votes

There's an easy way to do this. Given the orientation expressed as a quaternion (or 3x3 Matrix) compose it with a unit Y axis vector.

The resulting vec3's X and Z values are your forces. No need to involve Euler angles, which will almost certainly start hurting you as your approach a 90 degree angle from vertical.

0
votes

Finally solved it! I used

    GetComponent<Rigidbody> ().AddForce (RightCamera.transform.forward * angleX);
    GetComponent<Rigidbody> ().AddForce (-RightCamera.transform.right * angleZ);

And now the sphere rolls relative to the camera.