x


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));

        }
    }
more ▼

asked Dec 29 '10 at 04:36 PM

The 3D Ninja gravatar image

The 3D Ninja
194 12 12 22

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 gravatar image

The 3D Ninja
194 12 12 22

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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