Jordan Carroll
Jordan Carroll

Reputation: 33

Set rotation along Vector3 axis?

I'm building a controller with an arbitrary sense of "down", such that I can't use normal methods of camera movement.

I can move the camera on the up / right axes fine, but this creates unwanted "roll". I've looked around / tried a lot but can't find a good method of setting this camera rotation on an arbitrary "forward" axis without screwing up the other movement.

Any ideas?

Upvotes: 1

Views: 157

Answers (1)

Ruzihm
Ruzihm

Reputation: 20249

Use quaternion multiplication to rotate the camera around the arbitrary up axis (you can use Quaternion.AngleAxis to find this) and its local x axis according to the relevant input (mouse used here as example):

Vector3 myUp = Vector3.up; // set with arbitrary up
float rotationSpeed = 1f;

// ...

float horizMouseMove = Input.GetAxis("Mouse X") * rotationSpeed;
float vertMouseMove = Input.GetAxis("Mouse Y") * rotationSpeed
transform.rotation = Quaternion.AngleAxis(horizMouseMove, myUp) 
        * transform.rotation 
        * Quaternion.EulerAngles(-vertMouseMove, 0f, 0f);

Upvotes: 1

Related Questions