Question by
Glemau · Feb 26, 2017 at 01:27 PM ·
camerarotationclampclamped rotation
Clamping a camera with weird results
So I wrote a code, that should rotate an empty, and keep the camera's rotation within 20° of the empty's rotation:
public float mouseSensivety;
public GameObject cam;
public GameObject parentMesh;
private float camX = 0;
private float camY = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Moving Parent and Camholder
parentMesh.transform.Rotate (0,Input.GetAxis ("Mouse X") * Time.deltaTime * mouseSensivety, 0);
transform.Rotate (Input.GetAxis ("Mouse Y") * Time.deltaTime * mouseSensivety, 0, 0);
//Moving Cam
camX = Mathf.Clamp (cam.transform.eulerAngles.x, transform.localEulerAngles.x - 20, transform.localEulerAngles.x + 20);
camY = Mathf.Clamp (cam.transform.eulerAngles.y, parentMesh.transform.eulerAngles.y - 20, parentMesh.transform.eulerAngles.y + 20);
cam.transform.eulerAngles = new Vector3 (camX, camY, 0);
}
It is working somewhat fine, but at certain points it just snaps to another position(Look at the attached video). I believe it is in relation to the point where the rotation goes from 360° to 0° but I do not know how to overcome this issue.
Comment