- Home /
How to rotate my camera?
I just want to get my head around the basics. As soon as I use player.transform.Rotate to turn the player, the camera while using eulerAngles orbits the player. Can I compensate for this?
x += Input.GetAxis ("Mouse X");
x = Mathf.Clamp (x, -4, 4);
y += Input.GetAxis ("Mouse Y");
y = Mathf.Clamp (y, -12, 20);
player.transform.Rotate (0, x , 0);
x = 0;
transform.eulerAngles = new Vector3(y + mouseYOffset, 0, 0);
Because eulerAngles works perfectly (moving the cam up and down) when I comment out the Rotate part I'm guessing it's stuffing up because the camera is a child of the player and the rotation of the player is moving? If I use rotate instead of eulerAngles I have to put in if (angle > limit) { things to stop the player moving outside the bounds of the camera, which eulerAngles lets me use Mathf.Clamp... seems far better. So if anybody can help me get it to actually work that would be great :)
It seems you are making a FPS controller. Use the Standard Assets ins$$anonymous$$d as it has better camera movement.
Answer by knifeyau · Dec 22, 2016 at 04:31 PM
x = 0;
x += Input.GetAxis ("Mouse X");
x = Mathf.Clamp (x, -4, 4);
y += Input.GetAxis ("Mouse Y");
y = Mathf.Clamp (y, -12, 20);
player.transform.Rotate (0, x, 0);
transform.eulerAngles = new Vector3(player.transform.eulerAngles.x + y,
player.transform.eulerAngles.y, 0);
Grabbing the eulerAngles from the player and using them to offset the camera was the answer. Just figured it out on my own.... so I thought I would share this solution for anybody looking in the future.
~Knifey
Answer by ImFromTheFuture · Dec 22, 2016 at 01:06 PM
I had to write my own rotation script and this is what I came up with. You should modify this to suit your needs:
float rotDirection = 1;
public float rotSpeed, minAngle, maxAngle; // Angle clamps
public Transform target; // The gameobject you want to move
void Update {
float dX = Input.GetAxis ("Mouse X");
float dY = Input.GetAxis ("Mouse Y");
rotX += dY * rotSpeed * -rotDirection * Time.deltaTime;
rotY += dY * rotSpeed * rotDirection * Time.deltaTime;
if (rotX > 180)
rotX = rotX - 360;
rotX = Mathf.Clamp (rotX, minAngle, maxAngle);
target.eulerAngles = new Vector3 (rotX, rotY, 0f);
}
Your answer
Follow this Question
Related Questions
How do I make this Camera movement happen when I right click? 1 Answer
Forward and back movements with a camera emulating an isometric view 1 Answer
need help with roll a ball like game 0 Answers
How can i make the camera look up and down through keyboard keys? 2 Answers
Stop camera from stuttering when looking straight up or down 1 Answer