- Home /
Mouse orbit on click and drag/pan problem
I have an object targeted to a main camera, and simply want the user to rotate it on left click and drag, zoom with the scroll wheel, and pan with middle mouse and drag. At the moment, the object rotates every time the mouse is moved, rather than when the LMB is held down. I know it's a simple fix, but can't figure it out.
Also, the pan function just results in my object jittering back and forth. Suggestions for fix?
Thanks!
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
//From Unity forums for adding middlemouse zoom
var maxDist : float = 200;
var minDist : float = 30;
var zoomSpeed : float = 5;
var panSpeed = 0.3f;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance < maxDist){
distance += zoomSpeed;
transform.Translate(Vector3.forward * -zoomSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance > minDist){
distance -= zoomSpeed;
transform.Translate(Vector3.forward * zoomSpeed);
}
if (target && Input.GetMouseButtonDown(0)) {
}
//panning
else if (Input.GetMouseButton(2))
{
//grab the rotation of the camera so we can move in a psuedo local XY space
target.rotation = transform.rotation;
//target.Translate(transform.right * -Input.GetAxis("Mouse X") * panSpeed);
transform.Translate(transform.right*Time.deltaTime,Space.World);
target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
}
}
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 TheCodeMonkey · Jul 20, 2012 at 05:18 PM
if I am not mistaken:
if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
Is simply rotating based on the mouse itself every frame as long as there is a target you simply have to place a check for mouse input when you check for the target:
if (target && Input.GetMouseButtonDown(0))
Answer by YankinSheffield · Jul 20, 2012 at 06:18 PM
Thank you for answering, but I'm afraid I don't understand. I'm struggling with coding - I come from a much different background. I thought you meant that I just needed to move the "(target && Input..." line above the x+= Input.GetAxis to check for mouse input.I did that, but now it won't rotate at all, and the zoom in has stopped working. Can you please clarify? Thanks! New code below:
var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
//From Unity forums for adding middlemouse zoom
var maxDist : float = 200;
var minDist : float = 30;
var zoomSpeed : float = 5;
var panSpeed = 0.3f;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function LateUpdate () {
if (target && Input.GetMouseButtonDown(0)) {
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance < maxDist){
distance += zoomSpeed;
transform.Translate(Vector3.forward * -zoomSpeed);
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance > minDist){
distance -= zoomSpeed;
transform.Translate(Vector3.forward * zoomSpeed);
}
//panning
else if (Input.GetMouseButton(2))
{
//grab the rotation of the camera so we can move in a psuedo local XY space
target.rotation = transform.rotation;
//target.Translate(transform.right * -Input.GetAxis("Mouse X") * panSpeed);
transform.Translate(transform.right*Time.deltaTime,Space.World);
target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
}
}
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);
}
Sorry if I was unclear I did not mean rip it out and throw it up where I was commenting I simply meant to ADD the check in, and leave your other check where it was, your problem was that it was constantly rotating, the way you had the code before it would rotate every time the player moved the mouse no matter what. You needed the mouse check to make sure it worked properly. I will be home shortly and then I will be able to double check.
Also the reason it probably is not working currently is because your panning check is an else and is only being checked if (Input.GetAxis("$$anonymous$$ouse ScrollWheel") > 0 && distance > $$anonymous$$Dist) is not true. You needed to leave the check where it was before and not simply move it but add in the mouse input check. so your code would look almost exactly as it did originally with just one added check.
Answer by YankinSheffield · Jul 23, 2012 at 07:23 PM
Thanks so much for your help, TheCodeMonkey. The zoom and mouse orbit now work as expected. However, even with the "else" removed, the pan is still broken. But now the object just sort of dances off of the screen in a comical fashion. Will keep playing.
Your answer
Follow this Question
Related Questions
Mouse Orbit snapping issues 0 Answers
Mouse Orbit - Change From Mouse, to Keys 1 Answer
Mouse Orbit Improved (Rotating when Mouse Button is down) 2 Answers
Web player mouse tracking change between 2.6 and 3.0? 0 Answers
Smooth transition of camera targets 2 Answers