Jump to content

[SOLVED] Set constant speed randomly for group objects?


ekeimaja
 Share

Recommended Posts

So how can I set random speed for every object, so the acquired speed will stay? Now I have defined speed as constant, but it also increases the speed of every object.

SPEED = 3;

         this.enemies.forEachAlive(function(enemy){
             enemy.body.velocity.x += Math.random(SPEED + (SPEED/2));
         });

I tried first with Tanks -example style, where is defined max and min speed, but i didn't work.

Link to comment
Share on other sites

It was quick testing. First it was like this, but it did not work either.

enemy.body.velocity.x += Math.random() * (SPEED + SPEED/20);

Now I made it like this, but speed of objects increases and decreases all the time and they shake when moving.

enemy.body.velocity.x = 10 + Math.random()* 500;

 

Link to comment
Share on other sites

7 minutes ago, ekeimaja said:

Now I made it like this, but speed of objects increases and decreases all the time and they shake when moving.


enemy.body.velocity.x = 10 + Math.random()* 500;

 

^ this code sets a constant speed for the particular enemy's physics body; (assuming 0 accelleration and no collision) the enemy object will only change speed if you run this line again (or run a line like it)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...