# Position object instances evenly between two points

• I can't seem to get the math working for this, any help would be greatly appreciated.

I have two points A & B, and I calculate the distance between these, divide it by the length of the object I am wanting to spawn, then store it as an integer in a variable. The game then spawns a number of object instances equal to that variable.

What I am trying to do is spread all the currently spawned instances out evenly between the two points.

For reference, I am using a raycast every tick to get the positions and distance of the points. I'm trying to create a continuous laser weapon that uses small sprite segments for animation.

#### Tagged:

• Let's say you want to create 10 instances, the first instance should be at point A, and the last instance at point B:

``````Repeat 10 times
Create object at X=lerp(A_x, B_x, loopindex/9), Y=lerp(A_y, B_y, loopindex/9)
``````

If all 10 instances need to fit between points A and B:

``````For "n"=1 to 10
Create object at X=lerp(A_x, B_x, loopindex/11), Y=lerp(A_y, B_y, loopindex/11)
``````
• Thanks for that, I always forget about loopindex :/. The first part works perfectly for a one-shot between 2 points however, what I'm really looking for is how to maintain the line of sprites while the points move, for a continuous laser effect. Any ideas on that?

• To distribute existing sprites on a line, you can use "For each sprite" loop, and the same lerp expression.

Or you can even do this without a loop:

Sprite set position to X=lerp(A_x, B_x, self.IID/(Sprite.count-1), Y=lerp(A_y, B_y, self.IID/(Sprite.count-1))

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• I actually tried that first suggestion straight away after your first reply and it didn't work but my brain is a bit fried so I'll check again tomorrow. Thanks

• dop2000 I plugged it back in today and it worked, it was just an event sequence issue. I've added some other aspects and I think I'll do a tutorial for it as others might find it useful.

Thanks again!