- Home /
Camera Angle Conflict Pitch & Pan...I'm Lost
Hello All,
I'm trying to customize the Smooth-Follow camera to my needs but am having trouble understanding the camera angles. My script problems is as follows:
var pitch = 30;
function Update(){
transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * XYmousespeed);
This allows the camera to pan left and right via the mouse and works well with the camera following. The problem I'm having is setting the camera to pitch or tilt.
I added this to no avail:
transform.Rotate (pitch, 0, 0);
Obviously these two instructions conflict with each other but I cannot figure out how to make them "get along". I've tried everything I can think of but no matter what I do, I cannot get the first axis (x, I believe) to pan AND tilt - only one or the other. Any help would be greatly appreciated. Thank you for your time.
Answer by Bunny83 · Feb 22, 2011 at 01:17 AM
Ok, some notes in general: Input.GetAxis("Mouse ...") returns already delta values. That means it returns the amount of movement that happend in this frame. Don't use Time.deltaTime here. It would reduce the movement at high framerates and boost it on low framerates.
Now we fix the rotation. Transform.Rotate() rotates the object relative! If you call Rotate(0,0,0) nothing would change. If relativeTo is not set explicit to Space.World it defaults to Space.Self. That means you rotate you object relative to your local space.
I guess you want to rotate the camera around global y-axis and around local x-axis. This can be done with two calls to Rotate():
transform.Rotate(0, Input.GetAxis("Mouse X")*XYmousespeed, 0 ,Space.World);
transform.Rotate(Input.GetAxis("Mouse Y")*XYmousespeed, 0, 0 ,Space.Self);
An alternative would be to seperate both rotations and generate the Quaternions directly:
var Rot : Vector3 = transform.localEulerAngles;
Rot.y += Input.GetAxis("Mouse X") * RotationSpeed;
Rot.x += Input.GetAxis("Mouse Y") * RotationSpeed;
transform.localRotation = Quaternion.Euler(Rot);
ps. Don't set eulerAngles directly. They have to be within [0-360] and if you set them outside this range you will get errors.
First off, thank you for answering my question, I've been after a solution for four days. However, this didnt help me. I was looking to be able to rotate left and right with the mouse but also include a variable for the Inspector that would allow me to set the x axis up and down. Perhaps, this cannot be done? Ins$$anonymous$$d I cheated a little - I created an empty game.object as a child to my player.character and set it above and beyond my model, so my main.camera follows that ins$$anonymous$$d and it seems to work nicely.
I replaced my camera.orbit code with yours and it works well. I also appreciate the advice regarding Time.deltaTime and eulerAngles, I had no idea, thanks again.
Thank you ;) Well, you never said that you want an orbit camera. At least i didn't read that (even between the lines). And what you did is not cheating :D that's the easiest way to do orbiting. Unity calculates everything for you and you didn't have to deal with trigonometry to adjust the cam position.