- Home /
Rotate camera on mouse drag, minor glitch
I have a script I modified from the standard scripts mouseOrbit library. I have a scene and I just want people to be able to click and hold the left mouse button and while holding, drag to rotate the camera. It pretty much works exactly as it should except one minor issue. The very first time the scene loads, whenever I left click in the scene the camera rotation jumps noticeably to the right. Thereafter it works as expected, and only repositions if I click, hold and drag. But for some reason the first time I single click it jumps. Would appreciate someone's suggestion on what might be happening.
Here's the modified script. It should be pretty straight forward. It stores the x and y angle of rotation on startup, then on late update whenever the mouse is held down it gets the change in x and y axis and rotates based off that changed value.
Appreciate the help in advance!
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var xMinLimit = -20;
var xMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = gameObject.transform.eulerAngles;
y = transform.rotation.y;
x = transform.rotation.x;
// Make the rigid body not change rotation
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function LateUpdate () {
if (Input.GetMouseButton(0)) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
x = ClampAngle(x, xMinLimit, xMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;
}
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Answer by Bentoon · Jan 27, 2015 at 04:09 AM
Hey,
Maybe its another script or parameter on camera. I tried in a blank scene and there is no jumping
~be
O$$anonymous$$, thanks @Benton. I just started thinking I might have something somewhere else causing this jump. Appreciate your time. I'll come back and add more details if I have more to add.
Your answer
Follow this Question
Related Questions
How to force camera rotation? 1 Answer
Edited mouselook script rotation clamp not working 0 Answers
True Portal Effect 1 Answer