- Home /
Movement through an array over time
This is my current code, attached to a gameobject. It causes the gameobject to go from y position 10 to 22 but I want it to do that and then from 0 to 22, then from -20.5 to 22 and finally 13 to 22. Also, I want destination to instead point to the next array element(with the usual restriction to the array length). Thanks!
private var enemy001yMovement = new Array(); enemy001yMovement[0] = 10; enemy001yMovement[1] = 0; enemy001yMovement[2] = -20.5; enemy001yMovement[3] = 13; var destination = 22;
function Update() { for(var pointer in enemy001yMovement) { transform.position = Vector3(0,Mathf.Lerp(pointer,destination,Time.time),0); } }
You want to at least get rid of the for loop and maintain the index outside of Update. You want Update to act like the loop as it will run repeatedly over time.
Answer by skovacs1 · Aug 09, 2010 at 10:24 PM
Like Ben said, you want to track the index into the array and as update is called each frame, it will take care of acting over time.
Something like this:
Unity js
private var yWaypoints = new Array(); yWaypoints[0] = 10; yWaypoints[1] = 0; yWaypoints[2] = -20.5; yWaypoints[3] = 13; var index = 1; //Index into the array at the destination
//Called each frame function Update() { //If we've gone through the array, we're done if(index < yWaypoints.length) { //Move from the previous position in the array to the next over time transform.position.y = Mathf.Lerp(yWaypoints[index-1],yWaypoints[index],Time.time);
//If we've reached the destination, move to the next one
if(transform.position.y == yWaypoints[index]) index++;
}
}
Thanks a lot! Replacing the for loop with an if loop and using a .length array editor were the two differences i noticed first.
Eek, just tried it, it didnt work - got this error message : ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index 4
Yeah, sorry about that. Typed it up in like 5 $$anonymous$$utes. Should have incremented last. Fixed now.
Answer by Bampf · Aug 10, 2010 at 02:17 PM
It's worth taking the time to understand skovacs1's answer. If you don't understand when Update is called and how to use it, you won't get too far with Unity scripting.
For the specific problem of moving objects from place to place over time, which you will encounter over and over again, you might also want to look at iTween, which is free. The command to move a game object to position 0,22,0 over the next 2 seconds would look something like this: iTween.MoveTo(gameObject,Vector3(0,22,0),2);
You can queue up multiple movements, have the object start fast then slow down as it reaches the destination, and other effects. (Disclaimer: I'm planning to use it on my next project, and I expect it will save me a lot of time, but haven't have much hands-on with it yet.)
Yeh, of course - I find learning the correct placement of update function calling to be one of the more interesting parts of learning unity. I'm also new to javascript :) I've heard of itween, it looks useful and simple to implement but I wanted to learn how to do animations etc first
Sounds good. Hope I didn't sound patronizing! I just wanted to add the additional info about iTween, without see$$anonymous$$g to suggest that iTween replaces the usefulness of understanding Update. Carry on! :-)