- Home /
Question by
nicxl · Dec 18, 2016 at 11:30 AM ·
player movementcamera rotatecamera-look
Camera randomly turns left or right
I have the main camera as a child of my player, where the following code has it so that the player can look around. For some reason, I have this issue where if I walk into some stairs or if I walk into a door, the camera rotates by itself (left or right). I might just be stupid, but can someone help?
using UnityEngine; using System.Collections;
public class FirstPersonController : MonoBehaviour {
public float playerSpeed = 5.0f;
public float mouseSensitivity = 5.0f;
public float verticalRange = 60.0f;
float verticalRotation = 0f;
// Use this for initialization
void Start()
{
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
//Movement
float xAxis = Input.GetAxis("Horizontal") * playerSpeed;
float yAxis = Input.GetAxis("Vertical") * playerSpeed;
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
{
transform.Translate(xAxis * Time.deltaTime, 0, yAxis * Time.deltaTime);
GetComponent<AudioSource>().UnPause();
}
else
{
GetComponent<AudioSource>().Pause();
}
//Camera
float rotateHorizontal = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotateHorizontal, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp (verticalRotation, -verticalRange, verticalRange);
Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
}
}
Comment
Your answer
Follow this Question
Related Questions
How to make the player direction change with the camera rotation? 1 Answer
Player's movements doesn't match orbital camera 1 Answer
Rotating and Translating a GameObject in relation to an AR Camera in Unity/Vueforia 0 Answers
How do I get my forward movement to match with the direction of my camera? 1 Answer