- Home /
Question by
echl · May 16, 2016 at 01:45 PM ·
unity 5programmingcamera-movementcamera rotate
How to make camera mouse orbit not jittery?
I'm using the MouseOrbitImproved script which can be found with a quick Google search.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseOrbitImproved : MonoBehaviour {
public Transform target;
public float distance = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .0001f;
public float distanceMax = 1000f;
private Rigidbody rigidbody;
float x = 0.0f;
float y = 0.0f;
// Use this for initialization
void Start ()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate ()
{
if (target) //
{
if (Input.GetMouseButton (1)) {
//x += Input.GetAxis ("Mouse X") * xSpeed * distance * 0.02f;
x += Input.GetAxis ("Mouse X") * xSpeed * 0.02f; //I changed this from the above line
y -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f;
//y = ClampAngle (y, yMinLimit, yMaxLimit);
}
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast (target.position, transform.position, out hit))
{
distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
So the problem is, when I rotate the camera, it jitters/shakes. I'm not sure what causes this or how to fix it. Can anyone help me with this? Thank you!
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Camera following a cannonball that is mouse controlled. 3 Answers
How can I rotate my camera when player has rigidbody? 0 Answers
How to move a player automatically through a List of Waypoints? 1 Answer
Free Orbit Cam and Mouse Aim Cam aren't working together 0 Answers