- Home /
How to limit x-axis rotation of camera
What would be the simplest way to limit the rotation of the x-axis of my camera. I am trying to avoid using GetAxis and the mouse for moving the camera. And I want to avoid doing a an inside or outside loop when trying to look at the floor or ceiling. The biggest problem I'm having is the an Euler snaps back to zero.
Right now, learning the correct method to limit the x-rotation is more important than cleaning up the code, which I will do down the road.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour
{
public int rotateSpeed;
public int movementSpeed;
void FixedUpdate()
{
//Move player object forward along local z-axis
if(Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * Time.deltaTime * movementSpeed);
}
//Move player object backward along local z-axis
if(Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * Time.deltaTime * movementSpeed);
}
//Strafe left
if(Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * Time.deltaTime * movementSpeed);
}
//Strafe right
if(Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * Time.deltaTime * movementSpeed);
}
//Rotate/Look player up
if(Input.GetKey(KeyCode.UpArrow))
{
transform.Rotate(Vector3.left * Time.deltaTime * rotateSpeed);
}
//Rotate player down
if(Input.GetKey(KeyCode.DownArrow))
{
transform.Rotate(Vector3.right * Time.deltaTime * rotateSpeed);
}
//Rotate player left
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed, Space.World);
}
//Rotate player right
if(Input.GetKey(KeyCode.LeftArrow))
{
transform.Rotate(Vector3.down * Time.deltaTime * rotateSpeed, Space.World);
}
}
}
Your answer
Follow this Question
Related Questions
How to "clamp" a y rotation in unity? 0 Answers
How do you make a Tps cam with RotateAround around the X and Y axis without rotating on the Z axis? 1 Answer
What other methods are there for locking rotation of an object? 0 Answers
Swipe angle and Rotation of camera to movement 1 Answer
Camera moves with player in doom-like 0 Answers