x


For loop with Vector3.distance does the loop like 20 times per loop

so i have two vectors that are getting compared with Vector3.distance which is coming out right, but when i try to compare it in a for loop like this

for( var i = 0; i<= myDistance; i++){
instantiate(blahblah);
}

it goes completely crazy it instantiates like 20 or so every frame and it wont stop. i think this has to do with distance internally, but i dont know could some one with more knowledge help me out

more ▼

asked Feb 13 '12 at 04:25 AM

IMTRIGGERHAPPY9 gravatar image

IMTRIGGERHAPPY9
132 31 43 44

Could you show us where 'myDistance' is calculated? Keep in mind that this loop will execute fully once every frame- so if your object is 20 units away, it will always instantiate 20 things per frame.

Feb 13 '12 at 04:43 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

If that loop is in Update and your two objects are at a distance 20, obviously you'll got 20 instanciations every frame. You should do that somewhere else, triggered by some event (clic, collisions, etc).

By the way, when you compare vector, you should use the sqrMagnitude instead of distance, so you can spare yourself 2 sqrt.

more ▼

answered Feb 13 '12 at 04:49 AM

Berenger gravatar image

Berenger
11k 12 19 53

it is in update, but it only happens once per action, ie if i instantiate an object it compares that object with the previous one, it then doesn't go back to that until you instantiate the same object again? does this make sense? does it still matter then?

Feb 13 '12 at 04:58 AM IMTRIGGERHAPPY9

I'm not sure I get what you mean. Once per action ?

Feb 13 '12 at 06:40 AM Berenger
(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:

x1683
x355
x296
x55

asked: Feb 13 '12 at 04:25 AM

Seen: 440 times

Last Updated: Feb 13 '12 at 06:40 AM