# Problem with Orbit / Zooming

Hello guys.

I have two scripts to orbit and zoom around the objects.
They work fine but when I release the mouse button,
the camera goes to the first position. I want the camera
to stay where I left.

Can you help me? Thanks

ORBIT SCRIPT

``````    var target : Transform; //What to rotate around
var distance : float = 3.0; //How far away to orbit
var xSpeed : float = 250.0; //X sensitivity
var ySpeed : float = 250.0; //Y sensitivity

var resetTime : float = 1000.0; //How long to take to reset

private var startX : float = 0.0; //Start y rotation
private var startY : float = 0.0; //Start x rotation

private var x : float = 0.0; //Y rotation
private var y : float = 0.0; //X rotation

private var endX : float = 0.0; //Where to reset x from
private var endY : float = 0.0; //Where to reset y from

private var rotation : Quaternion; //Current orientation

private var resetting : boolean = false; //resetting?
private var resetTimer : float = 0.0; //How long we've been resetting

function LateUpdate() { //Every frame, do this as late as you can
if (target) {//There's a target
if(!resetting && Input.GetMouseButtonUp(0)) { //Released mouse button
resetting = false;
endX = x;
endY = y;
resetTimer = 0.0; //Reset the reset timer
}

if(!resetting) {
if (Input.GetMouseButtonDown(0)) { //Pushed mouse button
//Initialize the angles
var angles = transform.eulerAngles;
x = angles.y;
startX = x;
y = angles.x;
startY = y;
} //first time the button down

if(Input.GetMouseButton(0)) { //Mouse button is down
//Change the angles by the mouse movement
x += Input.GetAxis("Mouse X") * xSpeed * distance* 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
Reorient();
} //button held down
} //Camera moving
else { //Reset
resetTimer += Time.deltaTime; //add to the timer;
var amountReset = resetTimer / resetTime; //How far we are
x = Mathf.LerpAngle(endX, startX, amountReset); //Smooth angles
y = Mathf.LerpAngle(endY, startY, amountReset); //Smooth angles
Reorient();
if(resetTimer >= resetTime) resetting = false;
} //Reset
} //There's a target
} //LateUpdate

function Reorient() { //orient yourself
//Rotate the camera to those angles
rotation = Quaternion.Euler(y, x, 0);
transform.rotation = rotation;

//Move the camera to look at the target
transform.position = rotation * Vector3(0.0, 0.0, -distance)
+ target.position;
}
``````

ZOOM SCRIPT

``````    /*

This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.
There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
For every of those smoothed values we calculate the wanted value and the current value.

Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.

*/

// The target we are following

var target : Transform;

// The distance in the x-z plane to the target

var distance = 10.0;

// the height we want the camera to be above the target

var height = 5.0;

// How much we

var heightDamping = 2.0;

var rotationDamping = 3.0;

var mouseWheelDistanceRate = 5;

var mouseWheelHeightRate = 5;

// Place the script in the Camera-Control group in the component menu

function LateUpdate () {

// Early out if we don't have a target

if (!target)

return;

if (Input.GetAxis("Mouse ScrollWheel") != 0) {

Debug.Log(Input.GetAxis("Mouse ScrollWheel"));

distance -= Input.GetAxis("Mouse ScrollWheel") * mouseWheelDistanceRate;

height -= Input.GetAxis("Mouse ScrollWheel") * mouseWheelHeightRate;

}

// Calculate the current rotation angles

wantedRotationAngle = target.eulerAngles.y;

wantedHeight = target.position.y + height;

currentRotationAngle = transform.eulerAngles.y;

currentHeight = transform.position.y;

// Damp the rotation around the y-axis

currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

// Damp the height

currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

// Convert the angle into a rotation

currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

// Set the position of the camera on the x-z plane to:

// distance meters behind the target

transform.position = target.position;

transform.position -= currentRotation * Vector3.forward * distance;

// Set the height of the camera

transform.position.y = currentHeight;

// Always look at the target

transform.LookAt (target);

}
``````

You have to comment out the two lines about damping.
Also you need to explicitly declare the type of your javascript variables (float and quaternions), else you will get errors in the console.

Here is the corrected code:

``````#pragma strict
/*
This camera smoothes out rotation around the y-axis and height.
Horizontal Distance to the target is always fixed.
There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
For every of those smoothed values we calculate the wanted value and the current value.

Then we smooth it using the Lerp function.
Then we apply the smoothed values to the transform's position.

*/

// The target we are following

var target : Transform;

// The distance in the x-z plane to the target

var distance = 10.0;

// the height we want the camera to be above the target

var height = 5.0;

// How much we

var heightDamping = 2.0;

var rotationDamping = 3.0;

var mouseWheelDistanceRate = 5;

var mouseWheelHeightRate = 5;

// Place the script in the Camera-Control group in the component menu

function LateUpdate () {

// Early out if we don't have a target

if (!target)

return;

if (Input.GetAxis("Mouse ScrollWheel") != 0) {

Debug.Log(Input.GetAxis("Mouse ScrollWheel"));

distance -= Input.GetAxis("Mouse ScrollWheel") * mouseWheelDistanceRate;

height -= Input.GetAxis("Mouse ScrollWheel") * mouseWheelHeightRate;

}

// Calculate the current rotation angles

var wantedRotationAngle : float = target.eulerAngles.y;

var wantedHeight : float = target.position.y + height;

var currentRotationAngle : float = transform.eulerAngles.y;

var currentHeight  : float = transform.position.y;

// Damp the rotation around the y-axis

//currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

// Damp the height

//currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

// Convert the angle into a rotation

var currentRotation : Quaternion = Quaternion.Euler (0, currentRotationAngle, 0);

// Set the position of the camera on the x-z plane to:

// distance meters behind the target

transform.position = target.position;

transform.position -= currentRotation * Vector3.forward * distance;

// Set the height of the camera

transform.position.y = currentHeight;

// Always look at the target

transform.LookAt (target);

}
``````