# Change Camera Position

I am making a car game in Unity. I want the user to be able to change the camera position.
My current code gives back very weird values, that doesn’t make any sense.

This is my code:

``````#pragma strict
var currentAng = 2;

function Update () {
if(currentAng > 3){
currentAng = 1;
}
if (Input.GetKeyDown ("c")){
print ("C key was pressed");
Move();
}
}
function Move () {
if(currentAng == 1){
transform.position = Vector3.Lerp(this.transform.position, new Vector3(7,5367,-16293), 0.2);
}
if(currentAng == 2){
transform.position = Vector3.Lerp(this.transform.position, new Vector3(-7,3056,5861), 0.2);
}
if(currentAng == 3){
transform.position = Vector3.Lerp(this.transform.position, new Vector3(-964,3056,656), 0.2);
}
currentAng++;
}
``````

As defined here, Vector3.Lerp interpolates between the given points by the fraction given in the 3rd parameter. As shown in the example, if we want to “animate” a transition, Lerp should be called inside the Update method instead of doing it once when a key is pressed.

Here’s the modified script with other suggestions too

``````#pragma strict
var currentAng:int = 2; // define var types for better coding experience ;)
var startPos:Vector3;
var endPos:Vector3;
var speed:float = 1;
var time:float = 0;

function Start ()
{
// call Move in the beginning to initialize startPos and endPos
Move();
}

function Update ()
{
time += Time.deltaTime;

// calculate how many % of the distance is traveled base on set speed
var pctOfDistanceTraveled = time * speed;

// cap the traveled distance to be [0, 1]
if (pctOfDistanceTraveled > 1) {
pctOfDistanceTraveled = 1;
}

// move this object to a position where it has started from startPos and had traveled "pctOfDistanceTraveled" of the distanve to endPos
transform.position = Vector3.Lerp(startPos, endPos, pctOfDistanceTraveled);

if (Input.GetKeyDown ("c")){
print ("C key was pressed");
Move();
}
}

function Move ()
{
// reset move timer
time = 0;

currentAng++;
if(currentAng > 3) // if currentAng is changed only in Move, it only needs to be capped here (not every Update)
{
currentAng = 1;
}

// always start the moving from where we were when Move was called
startPos = this.transform.position;

// currentAng can be only one of these values at a time, hence "else if"
if(currentAng == 1){
// mark down where we want the transition to end up
endPos = new Vector3(7,5367,-16293);
}
else if(currentAng == 2){
endPos = new Vector3(-7,3056,5861);
}
else if(currentAng == 3){
endPos = new Vector3(-964,3056,656);
}
}
``````