- Home /
Question by
toomasb · Jun 08, 2015 at 10:56 AM ·
camerarotationquaternioneuleranglesclamp
Edited mouselook script rotation clamp not working
I'm using the standard assets MouseLook.cs script (but not the FPSCharacterController, because it doesn't have the features I need). I changed the MouseLook script to fit my needs but I can't seem to get the rotation clamp to work. The clamp flings my camera to the maximum clamp value as soon as it hits the clamp center.
What I'm trying to achieve is a first person camera, that can't turn the character (which is turned with the keyboard) and is clamped to, well, a more or less logical field of view when the character turns its head.
So the code at the moment is:
using System;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public GameObject player;
public float XSensitivity = 2f;
public float YSensitivity = 2f;
public float xMin = -90F;
public float xMax = 90F;
public float yMin = -90F;
public float yMax = 90F;
public bool smooth;
public float smoothTime = 5f;
private Quaternion m_CharacterTargetRot;
private Quaternion m_CameraTargetRot;
public void Start()
{
m_CharacterTargetRot = player.transform.localRotation;
m_CameraTargetRot = GetComponent<Camera>().transform.localRotation;
}
public void LookRotation(Transform player, Transform camera)
{
float yRot = Input.GetAxis("Mouse X") * XSensitivity;
float xRot = Input.GetAxis("Mouse Y") * YSensitivity;
m_CameraTargetRot *= Quaternion.Euler (-xRot, yRot, 0f);
m_CameraTargetRot = ClampRotationAroundXAxis (m_CharacterTargetRot);
transform.localRotation = Quaternion.Slerp (transform.localRotation, m_CameraTargetRot,
smoothTime * Time.deltaTime);
}
Quaternion ClampRotationAroundXAxis(Quaternion q)
{
float yDeg = m_CameraTargetRot.eulerAngles.y;
float xDeg = m_CameraTargetRot.eulerAngles.x;
float z = 0f;
float y = Mathf.Clamp (yDeg, yMin, yMax);
float x = Mathf.Clamp (xDeg, xMin, xMax);
q = Quaternion.Euler (x, y, z);
return q;
}
}
LookRotation, like originally, is called from the character controller script (that bit works)
Comment