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;
@script AddComponentMenu("Camera-Control/Mouse Orbit")
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);
}