- Home /
This question was
closed Apr 01, 2018 at 02:33 PM by
hexagonius for the following reason:
QAs about clamping camera rotations have been asked and answered many times before
Question by
$$anonymous$$ · Apr 01, 2018 at 02:18 PM ·
rotationquaternionmathf.clampfirst-person
I'm having trouble clamping the rotation of the camera?
Hello, I seem to be having some trouble integrating a function that I took from the MouseLook script that comes with importing the Characters package. Currently, it does not seem to be doing anything, but I would like it to limit the x of the camera rotation to only be -90 to 90. I'll put slashes after the important lines, the rest is working fine.
using UnityEngine;
using System;
using System.Collections;
public class CameraMouseLook : DavidBehaviour{
Vector2 MouseLook;
Vector2 SmoothV;
public float Sensitivity = 5;
public float Smoothing = 2;
public bool CanMove;
void Update(){
Camera.main.transform.localRotation = ClampRotationAroundXAxis (Camera.main.transform.localRotation);//
if (CanMove) {
var MD = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
MD = Vector2.Scale (MD, new Vector2 (Sensitivity * Smoothing, Sensitivity * Smoothing));
SmoothV.x = Mathf.Lerp (SmoothV.x, MD.x, Smoothing);
SmoothV.y = Mathf.Lerp (SmoothV.y, MD.y, Smoothing);
MouseLook += SmoothV;
Camera.main.transform.localRotation = Quaternion.AngleAxis (-MouseLook.y, Vector3.right);
transform.localRotation = Quaternion.AngleAxis (MouseLook.x, transform.up);
}
}
Quaternion ClampRotationAroundXAxis(Quaternion q)//
{//
q.x /= q.w;//
q.y /= q.w;//
q.z /= q.w;//
q.w = 1.0f;//
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);//
angleX = Mathf.Clamp (angleX, -90, 90);//
q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);//
return q;//
}//
}
Comment
You seem to be mixing Euler and Quaternions. This will never work.