- Home /
How to move along a group of waypoints at a certain speed?
I am trying to write code that records a players position as it moves, and when a key is pressed instantiates a copy that moves along the same path at the same speed. I have managed to make it work apart from moving at the same speed. when i press the key the character moves along the points to the current location in a fraction of a second. I have tried many variations but here is one version of the code:
var playerGO; var wayPoints = new Array (Vector3(0,0,0)); var playerLookAlike : Transform; private var playerLookAlike1: Transform;
function Start(){ playerGO = GameObject.FindWithTag("Player"); }
function Update () {
wayPoints.Add(playerGO.transform.position);
if (Input.GetKeyDown("z")){
playerLookAlike1 = Instantiate(playerLookAlike, Vector3(0,1.315133,0), playerLookAlike.rotation);
for (i = 1; i < wayPoints.length; i++){
playerLookAlike1.transform.position = Vector3.Lerp(wayPoints[i-1], wayPoints[i], 0.7);
print (playerLookAlike1.transform.position);
}
}
}
can anyone tell me how i can slow down the playerLookAlike so that is moves at the same speed that the player moved at?
Answer by ScroodgeM · Aug 08, 2012 at 08:32 PM
issue 1
function Update () { wayPoints.Add(playerGO.transform.position);
this is unusable data. Update() is called each frame rendered. it can be 300 frames per second or 3. so you will get 300 points or 3 - what will you do with all of them? wich time all points should play? and remember, that FPS is not a constant.
solution 1
store position in fixed time intervals. for example, each 0.1 second. code should look like:
//store time when we will store position float timeOfNextCheckpoint; //executes once at start void Start() { //initialize/reset time timeOfNextCheckpoint = Time.time; } //executes every frame void Update() { //check if it's time to store position if (Time.time >= timeOfNextCheckpoint) { //store position wayPoints.Add(playerGO.transform.position); //and calculate time for next storing timeOfNextCheckpoint += 0.1f; } }
issue 2
Update() is method that called once a frame. it must contains some simple work that you need EVERY frame. in your task you need to make a long-time operation, so you can only start it in Update, but not make it all. your code makes move through all points in one frame.
solution 2
void Update () { if (Input.GetKeyDown("z")) { playerLookAlike1 = Instantiate(playerLookAlike, Vector3(0,1.315133,0), playerLookAlike.rotation); //start a long operation StartCoroutine(ReplayCoroutine()); } } IEnumerator ReplayCoroutine() { for (i = 1; i < wayPoints.length; i++){ playerLookAlike1.transform.position = Vector3.Lerp(wayPoints[i-1], wayPoints[i], 0.7); print (playerLookAlike1.transform.position); //make a pause 0.1 sec yield return new WaitForSeconds(0.1f); } }
thanks a lot! i tried putting in the yield but it hadn't occurred to me to slow down the amount of coordinates being added to the way points variable. again thanks a lot!
Wondering if iTween (http://itween.pixelplacement.com) could be used here.
Answer by Kryptos · Aug 08, 2012 at 08:35 PM
You're changing the position in only one frame (one call to Update). You need to use a coroutine and yield between each change of position.
And your recording is also wrong because you have no idea how much time passed between each recorded waypoint. You could either use a coroutine with fixed time sample or record the deltaTime in a separate list.
Your answer
Follow this Question
Related Questions
How to move a gameobject with same speed 1 Answer
Determine transform.position of location between two waypoints on bezier path 0 Answers
Nested Prefab Issue 0 Answers
EditorGUILayout Array 1 Answer
object rotates when moving backward 1 Answer