- Home /
Using Lerp with 2d only moving partially
I am trying to make a very basic waypoint system. So I decided to use Lerp for changing between positions. The problem is that when I do this the object that should be moving doesn't "Lerp" to the place it is supposed to. So I should probably ask, using lerp, can it be called once within the update frame? Here's the code:
#pragma strict
var smooth : float = 3.0;
var waypointPos1 : Transform;
var waypointPos2 : Transform;
var hasFinished = false;
var currentPosition : Vector2;
function Start () {
currentPosition = transform.position;
}
function Update () {
if(hasFinished == false){
transform.position = Vector2.Lerp(currentPosition, waypointPos1.position, smooth * Time.deltaTime);
hasFinished = true;
}
}
I didn't figure out what the problem is, I am guessing it is because it only has the chance to move over 1 frame then it stops, but I am now using Vector2.moveTowards();
Answer by robertbu · Feb 15, 2014 at 09:00 PM
With MoveTowards() you can change line 24 to:
if (currentPosition == waypointPos1.position) hasFinished = true;
For Lerp() used this way, you need something a bit different because it can take a very long time to reach the goal after it is close to the goal. For Lerp() replace line 24 with:
if (Vector3.Distance(currentPosition, waypointPos1.position) < threshold) {
transform.position = waypointPos1.position;
hasFinished = true;
}
'threshold' is some smaller value like 0.1 that looks right for your particular setup.
Your answer
Follow this Question
Related Questions
How to get click position in Vector 2 ? 1 Answer
A node in a childnode? 1 Answer
When I move my arms back and forth, the player stops falling or moves upward 2 Answers
How to make it so enemies only move towards the player when the player is colliding with an object 1 Answer
Moving Player Up and Down in 2D 1 Answer