Help with camera move from A to B

Hello,

I have tried to adapt this script I found - credit to this link [can someone explain how using Time.deltaTime as 't' in a lerp actually works? - Unity Answers][1]

I want to be able to move a camera from any given world position, to a new world position, defined by variable pointB, and only when a key is pressed.

The problem I have with the current script, is that the camera always defaults back to 0,0,0, world space, before moving to pointB, rather than simply moving from it’s current location.

Furthermore, I have to repeatedly press ‘J’ for the move to occur - it doesn’t move on 1 single key press.

Here’s the code I have so far:

var myCamera : GameObject;
private var pointA : Vector3 = myCamera.transform.localPosition;
var pointB:Vector3;
var time:float = 10.0;
private var i:float = 0.0;
private var rate:float = 0.0;

function Update () {
if (Input.GetKeyDown("j")) {
	goMove ();
	}
}

function goMove () 
{   
    MoveObject(this.transform, pointA, pointB, time);
}

function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) 
{
    rate = 1.0/time;
    if (i < 1.0)
    {
        i += Time.deltaTime * rate;
        thisTransform.position = Vector3.Lerp(startPos, endPos, i);
    }
}

I’ve tried to use both Transform.localPosition and Space.Self where var pointA is defined, but it hasn’t worked so far.

Btw, I am really new to scriptiing :slight_smile:

Any help really appreciated.

You should use FSM for this. The problem is you call move function whenever J is pressed.When j is pressed movement function called for just once because of that you keep pressing J. What you should do according to me is:

define an enum corresponds to camera state like:

public enum CameraState
{
move,
idle
}

after this definition you should keep current state of camera:

public CameraState status=CameraState.idle;

when j is pressed change its state to CameraState.move;

in update function chech camera’s state:

void Update()
{
  switch(status)
 {
   case CameraState.idle:break;
   case CameraState.move:goMove();break;
   default:break;
 }
 if(Input.GetKeyDown(Keycode.J))
 {
   state=CameraState.move;
 }

}

When camera moves enough to position you want change its status to CameraState.idle

See MoveObject, specifically the second form of the Translation function.

You could also try out this method.