- Home /
Adjust camera rotation while looking at a target
Hi,
I currently have my camera looking at a target using LookTo(). I need to be able to adjust the rotation of the camera based off mouse mouse movement. Basically, I want the camera to look at a target, but the user can move the mouse to look around that target. I don't want the camera to move, just rotate a certain degree to the left/right, up/down, while still facing the same target.
Here's what I have to rotate the camera based off mouse movement. This works for rotation based off mouse move, but since I'm setting the lookTo towards the mouse, I'm losing the desired camera direction.
I'd greatly appreciate some help! Thanks.
void RotateCamera(Vector2 input) {
if(isRotateCamera)//only rotate if camera is not moving along path
{
Vector3 MouseWorldPosition = camera.ScreenToWorldPoint(new Vector3(input.x, input.y, 100));
//apply rotation to camera
MouseWorldPosition.Normalize();
transform.LookAt(MouseWorldPosition * 20, transform.position);
transform.rotation = Quaternion.Euler(new Vector3(transform.eulerAngles.x, transform.rotation.eulerAngles.y, 0));
}
}
Answer by The 3D Ninja · Dec 29, 2010 at 09:15 PM
Found a solution. I implemented the MouseLook code from the standard assets into my class. Here's the method that is called to rotate the camera.
void RotateCamera(Vector2 input) {
if(isRotateCamera)//only rotate if camera is not moving along path
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + input.x * sensitivityX;
rotationY += input.y * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, input.x * sensitivityX, 0);
}
else
{
rotationY += input.x * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
}
Your answer
Follow this Question
Related Questions
Quaternion.LookRotation is making my object rotate around a point? 1 Answer
How to setup camera look at mouse on right click 0 Answers
Camera Rotation 1 Answer
LookAt Camera over 90 degree 1 Answer
Third person animation 0 Answers