# Camera Orbit, with Left Click, and Zoom problem

Hello,

I’m trying to manipulate the camera orbit script to work as I want it to, but I’m stuck.

What I want is for the user to click and drag left mouse button to rotate, and use the scroll wheel to zoom.

So I’ve taken the orbit script, added in the left mouse button to rotate. Now the problem is the zoom requires the LMB to be down while scrolling.
I’ve tried separating the code, but it’s still reacting the same. Does any one have any ideas or solutions?

``````var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var distanceMin = 3;
var distanceMax = 15;
private var x = 0.0;
private var y = 0.0;

//partial class MouseOrbit { } //NEW
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function LateUpdate () {
if (target && Input.GetMouseButton(0)) {
//if (target) {
x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
//distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
//var hit : RaycastHit;
//if (Physics.Linecast (target.position, transform.position, hit)) {
//        distance -=  hit.distance;
//}
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}

}

function Update() {

if (target) {

distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);

var hit : RaycastHit;
if (Physics.Linecast (target.position, transform.position, hit)) {
distance -=  hit.distance;
}

}

}
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);
}
``````

Cheers,
Dean

you have this, if there is a target and the left mouse is pushed down

``````if(target && Input.GetMouseButton(0))
{
....rotate stuff....
... zoom stuff...
}
``````

you want

``````if(target)
{
if(Input.GetMouseButton(0))
{
....rotate stuff....
}
... zoom stuff...
}
``````

I’m still not getting it to work, and I’ve change it to the structure you suggest, but I still need to hold the LMB while scrolling to zoom

``````var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var distanceMin = 3;
var distanceMax = 15;
private var x = 0.0;
private var y = 0.0;

function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (GetComponent.<Rigidbody>())
GetComponent.<Rigidbody>().freezeRotation = true;
}
function LateUpdate () {

if (target)
{
{

distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);

var hit : RaycastHit;
if (Physics.Linecast (target.position, transform.position, hit)) {
distance -=  hit.distance;
}

}

if (Input.GetMouseButton(0))
{

x += Input.GetAxis("Mouse X") * xSpeed * distance* 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;
}

}
}
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);
}
``````

Any ideas?

Cheers,
Dean

these 4 lines are the actual “work out and set position and rotation” they need to happen outside the if for mouse input

``````        var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
``````

note, you need to declare the vars “distance” “y” and “x” outside of any of the if’s otherwise you’ll run into errors if there is no target or if the mouse button wasn’t pressed as you’ll be trying to use an undeclared var. You’ll also need to figure out values for if the mousescroll doesn’t happen or if the mouse button isn’t pressed.
so,

slightly more complete psuedocode

``````get current values for distance, y, x
if there is a target
{
if there is scroll
{
update distance as needed
}
if there is rotation
{
update x, y as needed
}
}
work out the position and rotation
set the position and rotation
``````