# Adjust camera rotation while looking at a target

 0 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)); } } `````` more ▼ asked Dec 29 '10 at 04:36 PM The 3D Ninja 194 ● 12 ● 12 ● 22 SisterKy 2.4k ● 33 ● 41 ● 59 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

1 answer: sort voted first
 0 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); } } } `````` more ▼ answered Dec 29 '10 at 09:15 PM The 3D Ninja 194 ● 12 ● 12 ● 22 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
Your answer

### 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:

x3002
x2166
x984
x329
x286

asked: Dec 29 '10 at 04:36 PM

Seen: 1283 times

Last Updated: Dec 29 '10 at 04:36 PM