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 ![]()
Any help really appreciated.