x


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);
}
}
more ▼

asked Aug 09 '10 at 07:53 PM

maggot gravatar image

maggot
190 26 28 36

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.

Aug 09 '10 at 09:56 PM Ben 2

Thanks, i think the answer is given more fully below

Aug 10 '10 at 05:27 PM maggot
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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++;
    }
}
more ▼

answered Aug 09 '10 at 10:24 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

Thanks a lot! Replacing the for loop with an if loop and using a .length array editor were the two differences i noticed first.

Aug 10 '10 at 05:21 PM maggot

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

Aug 10 '10 at 06:56 PM maggot

Yeah, sorry about that. Typed it up in like 5 minutes. Should have incremented last. Fixed now.

Aug 12 '10 at 02:02 PM skovacs1
(comments are locked)
10|3000 characters needed characters left

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.)

more ▼

answered Aug 10 '10 at 02:17 PM

Bampf gravatar image

Bampf
5k 8 19 49

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

Aug 10 '10 at 05:26 PM maggot

Sounds good. Hope I didn't sound patronizing! I just wanted to add the additional info about iTween, without seeming to suggest that iTween replaces the usefulness of understanding Update. Carry on! :-)

Aug 11 '10 at 03:01 AM Bampf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1366
x1359
x572
x294
x5

asked: Aug 09 '10 at 07:53 PM

Seen: 1152 times

Last Updated: Nov 25 '11 at 11:47 PM