- Home /
Camera is constantly locked on the Y-Axis
Hello, fellow Unity-users,
i'm having a very strange bug in my current game. Whenever i start the game and look around, the camera always seems locked on the y-axis(horizontal rotation). The playermodel rotates just fine but the camera does'nt rotate with it. Vertical rotation works just fine... so i have no idea why this happens.
using UnityEngine;
using System.Collections;
public class CamLook : MonoBehaviour
{
public enum LookType
{
Horizontal,
Vertical,
Both
}
public LookType lookType;
//
public bool canLook = true;
//
public float lookSens;
//
public int minVertical = -90, maxVertical = 90;
//
float xRotation, yRotation;
//
private float smoothing = 0.02f;
void Update()
{
if (canLook)
{
if (lookType == LookType.Horizontal)
{
xRotation += Input.GetAxis ("Mouse X") * (lookSens * smoothing);
transform.rotation = Quaternion.Euler (0, xRotation, 0);
}
else if (lookType == LookType.Vertical)
{
yRotation -= Input.GetAxis ("Mouse Y") * (lookSens * smoothing);
yRotation = Mathf.Clamp(yRotation, minVertical, maxVertical);
transform.rotation = Quaternion.Euler (yRotation, 0, 0);
}
else
{
xRotation += Input.GetAxis ("Mouse X") * (lookSens * smoothing);
yRotation -= Input.GetAxis ("Mouse Y") * (lookSens * smoothing);
yRotation = Mathf.Clamp(yRotation, minVertical, maxVertical);
transform.rotation = Quaternion.Euler (yRotation, xRotation, 0);
}
}
}
}
i set it up like this: The player itself is an empty gameobject with the CamLook script attached to it (set to horizontal movement). the camera is a child of the player(set to vertical movement)
Thanks in advance :)
Answer by tanoshimi · Jan 07, 2017 at 10:41 PM
"...the camera is a child of the player(set to vertical movement)..."
Surely that explains why the camera only rotates vertically, right? If you want it move both horizontally and vertically, change the LookType to Both.
doing this makes the camera all jittery... If the camera is the child of the player, shoud'nt it rotate simultaneously to the horizontal rotation of the player?
No, because on line 41 you're specifying that, if LookType is vertical, the xRotation should always be 0:
transform.rotation = Quaternion.Euler (yRotation, 0, 0);