x


Rotate camera 360 degrees?

Hi,

In my game, you should be able to look around 360 degrees. Now this seems to be a bit of a problem, because Unity won't let me change the camera's Upvector. I tried this for my player:

transform.Translate(new Vector3(Input.GetAxis("Horizontal") * Time.deltaTime * 5, 0, Input.GetAxis("Vertical") * Time.deltaTime * 5));

transform.eulerAngles += new Vector3(0, Input.GetAxis("Mouse X") * Time.deltaTime * 500, 0);

transform.LookAt(transform.position + transform.forward, transform.up);

Aand then this for my camera:

transform.eulerAngles = new Vector3(transform.eulerAngles.x, target.transform.eulerAngles.y, target.transform.eulerAngles.z);

transform.eulerAngles += new Vector3(-Input.GetAxis("Mouse Y") * Time.deltaTime * 500, 0, 0);

transform.LookAt(transform.position + transform.forward, transform.up);

But, when my player is standing on the ceiling or some other place where he's turned around, the camera completely freaks out. So I tried this for my player:

transform.rotation *= Quaternion.Euler(new Vector3(0, Input.GetAxis("Mouse X") * Time.deltaTime * 500, 0));

And this for my camera:

transform.rotation *= Quaternion.Euler(new Vector3(-Input.GetAxis("Mouse Y") * Time.deltaTime * 500, Input.GetAxis("Mouse X") * Time.deltaTime * 500, 0));

This is actually a lot better, but not perfect. I can now turn left and right, up and down correctly, even if my character and thus the camera is upside down. But when I'm watching upward and then try to look left or right, my camera acts as if it is falling over. Now I know this is happening because of the axis over which the camera rotates, but how can I fix it?

more ▼

asked Nov 06 '10 at 01:50 PM

Secret-V gravatar image

Secret-V
1 1 1 1

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

1 answer: sort voted first

I though you would get an error on this one: "transform.eulerAngles += ", because transform.eulerAngles returns a copy euler angles, so it doesn't get set back. Can you try this:

Vector3 rot = transoform.eulerAngles;
rot += ...
transform.eulerAngles = rot;

Why do you need to do this:

transform.LookAt(transform.position + transform.forward, transform.up);

I don't see how it changes your transform.

more ▼

answered Nov 08 '10 at 11:05 PM

Paulius Liekis gravatar image

Paulius Liekis
7.3k 16 24 45

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

x2993
x2159
x329
x3
x2

asked: Nov 06 '10 at 01:50 PM

Seen: 2110 times

Last Updated: Nov 06 '10 at 01:50 PM