# MouseOrbit Camera Lerp

I’m making a game where you control 2 characters, when u press “E”, it switch between them.
For my camera I’m using unity’s MouseOrbit script. When I switch the character been controlled I change the focus of my camera, but it jumps to the new position.
How can I make it smoothly “lerp” to that position? Codes would help, I’m not used to java, I usually use C#.

That’s the code as it is now:

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

var player1 : Transform;
var player2 : Transform;

var activePlayer : Transform;

function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

target = player1;

// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}

function Update(){
SwitchFocus();
}

function SwitchFocus(){
if (Input.GetKeyDown(KeyCode.E)){
Debug.Log("oi");
if(activePlayer == player2){
activePlayer = player1;
}else{
activePlayer = player2;
}
SetActivePlayer();
}
}

function SetActivePlayer(){
target = activePlayer;
}

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

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

What is it, exactly, that you want to achieve? You want the target of the mouseOrbit to smoothly translate to the position of the new character. So, why modify the orbit script at all?

All you need to do is set the orbit script to follow an empty transform, and move that around in a smooth fashion. After all, the mouse orbit doesn’t need to know about how its target is being moved, so splitting the scripts up makes sense.

On this new object, add a script that works something like this:

``````public Transform initialTarget;
Transform currentTarget;
Transform nextTarget;

void Start()
{
currentTarget = initialTarget;
transform.parent = currentTarget;
}

public void MoveToNewTarget(Transform newTarget, float time)
{
transform.parent = null;
nextTarget = newTarget;
StartCoroutine(MoveCoroutine(time));
}

IEnumerator MoveCoroutine(float timeToMove)
{
float currentTime = 0;
while(currentTime < timeToMove)
{
float currentDistance = currentTime / timeToMove;
transform.position = Vector3.Lerp(currentTarget.position, nextTarget.position, currentDistance)
transform.rotation = Quaternion.Slerp(currentTarget.rotation, nextTarget.rotation, currentDistance);
currentTime += Time.deltaTime;
yield return null;
}
currentTarget = nextTarget;
nextTarget = null;
transform.position = currentTarget.position;
transform.rotation = currentTarget.rotation;
transform.parent = currentTarget;
}
``````

Whenever you want to switch to a new target, just call `MovableTarget.MoveToNewTarget(nextTransform, 5);` to move to transform ‘nextTranform’ ovef 5 seconds.