- Home /
Clamp Quaternions Rotation for camera
I'm pretty new to the whole 'Quaternion' bussines and I'm trying to keep the camera from rotating all the way up or down. So I figured 'Mathf.Clamp' would be my best shot, but I can't figure this one out.
public float rotateVel = 100;
public float inputDelay = 0.1f;
public float yMin;
public float yMax;
private float yRot;
public float turnInput;
private Quaternion targetRotation;
// Use this for initialization
void Start () {
targetRotation = transform.rotation;
turnInput = 0;
}
// Update is called once per frame
void Update () {
turnInput = Input.GetAxis("Mouse Y");
if (Mathf.Abs(turnInput) > inputDelay)
{
targetRotation *= Quaternion.AngleAxis(rotateVel * -turnInput * Time.deltaTime, Vector3.right);
}
transform.rotation = targetRotation;
}
I was hoping somebody could help me out here.
Thanks in advance :D
Answer by Bioinformatizer · Oct 25, 2016 at 09:52 PM
This was a problem for me also. I have a fix, but the coding elegance is lacking.
// CameraOrbit - Bioinf
using UnityEngine;
using System.Collections;
public class CameraOrbit : MonoBehaviour
{
public float minAngle = -80;
public float maxAngle = 80;
public float minDistance = 3;
public float MaxDistance = 10;
public float currentDistance = 5;
public float zoomSensitivity = 2;
[Space]
public GameObject currentTarget;
private bool rightDown;
private float mouseScroll;
private float lastDistance;
private Vector2 rightClickMouseDelta = Vector2.zero;
private Camera cam;
void Start()
{
cam = gameObject.GetComponent<Camera>();
lastDistance = currentDistance;
if (currentTarget == null)
{
CameraFocus();
}
}
void CameraFocus()
{
// decides the camera target and ortho and zoom level
}
void Update() {
GetInput();
if (rightDown) // && spinForce > currentSpeed
{
//StartCoroutine(ChangeOrbit());
ChangeOrbit();
}
if (mouseScroll != 0)
{
lastDistance = currentDistance;
currentDistance += (-mouseScroll * Time.deltaTime * zoomSensitivity);
ZoomCamera();
}
}
void GetInput()
{
if (Input.GetMouseButton(1) && rightDown)
{
rightClickMouseDelta += new Vector2(Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
}
else if (Input.GetMouseButton(1) && !rightDown)
{
rightDown = true;
rightClickMouseDelta = Vector2.zero;
}
else
{
rightDown = false;
}
mouseScroll = Input.GetAxis("Mouse ScrollWheel");
}
void ZoomCamera()
{
gameObject.transform.position *= (currentDistance - lastDistance + 1.0f);
gameObject.transform.position = Vector3.ClampMagnitude(gameObject.transform.position, MaxDistance*3.14f);
}
//IEnumerator ChangeOrbit()
void ChangeOrbit()
{
Vector3 angles = transform.eulerAngles;
angles.z = 0;
transform.eulerAngles = angles;
if (currentTarget)
{
transform.RotateAround(currentTarget.transform.position, Vector3.up, rightClickMouseDelta.x);
// Lock Rotation Function
float deltaAngle = rightClickMouseDelta.y * 0.25f;
if ((deltaAngle + angles.x) > maxAngle && (deltaAngle + angles.x) < 180 || (deltaAngle + angles.x) < minAngle + 360 && (deltaAngle + angles.x) > 180)
{
deltaAngle = 0;
}
transform.RotateAround( currentTarget.transform.position,
new Vector3(Mathf.Sin(Mathf.Deg2Rad * (angles.y + 90)), 0f, Mathf.Cos(Mathf.Deg2Rad * (angles.y + 90))),
deltaAngle);
gameObject.transform.LookAt(currentTarget.transform);
}
else {
transform.RotateAround(Vector3.zero, Vector3.up, rightClickMouseDelta.x);
// Lock Rotation Function
float deltaAngle = rightClickMouseDelta.y * 0.25f;
if ((deltaAngle + angles.x) > maxAngle && (deltaAngle + angles.x) < 180 || (deltaAngle + angles.x) < minAngle + 360 && (deltaAngle + angles.x) > 180)
{
deltaAngle = 0;
}
transform.RotateAround( Vector3.zero,
new Vector3(Mathf.Sin(Mathf.Deg2Rad * (angles.y + 90)), 0f, Mathf.Cos(Mathf.Deg2Rad * (angles.y + 90))),
deltaAngle);
gameObject.transform.LookAt(Vector3.zero);
}
}
}
Ignore CameraFocus here and set your own gameObject as target if you want.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
how to clamp y axis with Quaternion.Euler in unity 1 Answer
Trouble with Camera Rotation Math 2 Answers
Weird shake when I try to rotate player according to Virtual Cameras rotation. 0 Answers
Transform rotation and position on key input: rotation only working the first time 2 Answers