- Home /
How to force camera rotation?
Very simple here, I know... But I think I'm lost.
I've coded a script to control my camera and here is what I do to handle mouse control:
void LateUpdate() {
if (camlock == false) {
_x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
_y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
_myTransform.rotation = rotation;
}
}
It works pretty fine. But when player will press a button I'd like to force the camera position and orientation. I've tried using:
transform.rotation = Quaternion.LookRotation(my_new_rotation_vector3);
But the rotation is not good (due to the lateUpdate method computing _x and _y according to Input.GetAxis() I think).
Any idea on how I should do to force camera rotation? should I use quaternions? If yes how?
Thank you.
So do you have a vector you want the camera to look at or an actual orientation?
I must admit that I don't know the difference between both. But yes, I have a vector3 defining where the camera has to look.
And the Vector3 you have is a position not the description of the rotation around 3 axes?
Quaternion.LookRotation() looks down a vector which is a direction.
Answer by whydoidoit · Jun 19, 2012 at 10:37 AM
This will modify the current camera rotation by the mouse, allowing you to set another rotation. Drop your _x and _y variables.
void LateUpdate() {
if (camlock == false) {
var rot = transform.rotation.eulerAngles;
rot.x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
rot.y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
_myTransform.rotation = Quaternion.Euler(rot);
}
}
Thanks. I'm a bit ashamed it was so obvious and I didn't think of it.
Your answer
Follow this Question
Related Questions
A little help with this camera script please? 3 Answers
Lerp Third Person Camera Rotation 1 Answer
Camera rotation script logic error? 0 Answers