x


Keep camera horizontal with RotateAround

Hello,

I've made a script to rotate the camera around an object with touch-gesture (iOS):

var x = Input.touches[0].deltaPosition.x * rotateSpeed * Time.deltaTime;

var y = Input.touches[0].deltaPosition.y * rotateSpeed * Time.deltaTime;

transform.RotateAround (gameObject.Find("Cube").transform.position, camera.transform.right, -y); transform.RotateAround (gameObject.Find("Cube").transform.position, camera.transform.up, x);

The camera rotates around the Cube. When I activate one of the two lines separately they work ok.

With only first line active swiping Up-down, rotates the camera around the cube vertically.

With only the second line active swiping left-right, moves the camera around the cube horizontally.

But if I activate them both the movement is not like expected, the camera doesn't keep level with the horizon.Instead it dolly's around. Basically what I'm after is the movement created with the Unity MouseOrbit.js-script (which is only for mouse-movement). But I can't get it working in combination with my script.

Any suggestions? Is it my combining the 2 movements wrong or do I need to build in some sort of restriction? Any advice is greatly appreciated. David

more ▼

asked Mar 04 '11 at 06:20 PM

David 25 gravatar image

David 25
31 4 4 9

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

2 answers: sort voted first

The second parameter of RotateAround is the axis of rotation. Make sure that is always vertical, e.g. instead of camera.transform.right and up, just use Vector3.right and Vector3.up. You may need to clamp one of the values to something (-90,90) to prevent weirdness at the top/bottom.

more ▼

answered Mar 05 '11 at 12:52 PM

Molix gravatar image

Molix
4.8k 16 27 66

Thank you for the advice. I'll try it asap.

Mar 05 '11 at 08:37 PM David 25
(comments are locked)
10|3000 characters needed characters left

The above does not work for me, try this if you have the same results:

    // fetch the users mouse motion for this frame
    y = -Input.GetAxis( "Mouse Y" ) * ySpeed;
    x = Input.GetAxis( "Mouse X" ) * ySpeed;

    // now adjust cameras rotation around target player based on the mouse x,y movement
    transform.RotateAround( target.position, target.right, y );
    transform.RotateAround( target.position, target.up, x );
    transform.rotation = Quaternion.Euler( transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0f );
more ▼

answered Mar 09 at 01:04 AM

Lypheus gravatar image

Lypheus
292 28 35 49

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

x2996
x1958
x580
x101
x87

asked: Mar 04 '11 at 06:20 PM

Seen: 2155 times

Last Updated: Mar 09 at 01:04 AM