- Home /
Lerp Movement Pauses after frame
Hey everyone, I've been trying to sort this out for the last 6 hours. Maybe you can help me. In my Update function I have an input cmd:
if(Input.GetButtonDown("Walk")){
CurrentUnit = true;
Play();
CurrentUnit = false;
}
Then in my Play function:
function Play () {
//Debug.log(Team1Unit1Origin.GetType);
//collect all waypoint game objects
var waypoints = new Array (GameObject.FindGameObjectsWithTag("Waypoint"));
//loop through waypoint array
for (i = 0; i < waypoints.length; i++){
//variable for waypoint vector
var newposition = waypoints[i].transform.position;
//set time scale
t = Time.deltaTime * 5;
//move unit to first waypoint in array
Team1Unit1.transform.position = Vector3.Lerp(Team1Unit1.transform.position, newposition, Time.DeltaTime * 5);
}
The problem I'm having is that the Unit stops after each frame, so I have to keep tapping the Walk button for it to continue.
I'm not quit sure what I'm doing wrong, if anyone could help that would be great.
Answer by aldonaletto · Sep 02, 2011 at 12:27 PM
If you want the object Team1Unit1 to move to all waypoints in sequence when you press "walk", Play must be a coroutine where the object Lerps from the current position to the new waypoint at a defined speed, then change the waypoint and Lerps to it, and so on till the last one. The variable walking is set at the beginning of Play, and only cleared at the end - it's used to avoid other Play cycles being initiated while one is running.
I changed the creation of the waypoints array to Start, because Find operations are too slow (the docs say we should use them only at Start/Awake).
var speed: float = 3; // speed = 3 m/s private var waypoints: GameObject[];
function Start(){ waypoints = GameObject.FindGameObjectsWithTag("Waypoint"); }
function Update(){ if (Input.GetButtonDown("Walk")){ Play(); // starts Play cycle and returns immediately } }
private var walking = false;
function Play () { if (walking) return; // don't start a new cycle until this one has finished walking = true; // cycle started //loop through waypoint array for (i = 0; i < waypoints.length; i++){ // get next waypoint var newposition = waypoints[i].transform.position; // save start position var oldPos = Team1Unit1.transform.position; // calculate factor to walk at defined speed var spd = speed / Vector3.Distance(oldPos, newposition); for (var t:float=0; t<1;){ // sweep from oldPos to newPosition t = Time.deltaTime * spd; // ensures motion at defined speed // move object a little each frame Team1Unit1.transform.position = Vector3.Lerp(oldPos, newposition, t); yield; // return here next frame } } walking = false; // cycle ended }
Answer by Finjitzu · Sep 02, 2011 at 12:36 PM
HaHa yeah I just got it to work after finding this post:
http://answers.unity3d.com/questions/160549/click-on-object-and-move-with-lerp.html
Seriously after like 6 hours of trying different things. I know how to program, but I don't know how games work :P
I'm not sure if I can populate the waypoint array in the function start though. The player is able to spawn as many waypoints as they like. Then the play function gathers the waypoints and moves the player. Think Frozen Synapse. Perhaps I can populate the array on the user pick? Then send that array to the MainGameScript.
Thanks so much for your help, I'm loving this!
You actually don't need to do that at Start - the docs recommend this because FindGameObjectsWithTag is a slow operation, but it's ok to use it at the Play routine beginning since it will be called rarely.
Your answer
Follow this Question
Related Questions
Getting a 2d Sprite to move over time to an Array 1 Answer
Move continuously through array of Vectors 1 Answer
Jump with mathf.lerp problem 2 Answers
Smooth rotation... 1 Answer