Sign in to follow this  
ekeimaja

[SOLVED] Set constant speed randomly for group objects?

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.

Share this post


Link to post
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;

 

Share this post


Link to post
Share on other sites

Yes if you change the speed every frame (or every few frames) the speed will change every frame (or every few frames).

If you want to set the speed and have it not change. Ummm.... don't change it!

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.