Sign in to follow this  
zorocke

Random movement for each object

Recommended Posts

I would like for my enemy objects to have a bit of a random movement using the code below.


Random random = new Random((int)time);


//Sway our ship moving left sometimes to moving right sometimes
int offset = random.Next(70, 120);
double degrees = time * offset;
double radians = MathHelper.ToRadians((float)degrees);
float sin = (float)Math.Sin(radians);
//Create a random range to all
if (sin > 0)
m_move.X = m_speed;
else if (sin < 0)
m_move.X = -m_speed;


My enemy objects are stored in an array, and the code above is the in a method called for each enemy in the array.

The result is all my enemy objects follow the exact same movements.

Share this post


Link to post
Share on other sites
Need a bit more context. Are you instantiating an instance of Random for each ship using the same seed? Whats the resolution on the time stored in the time variable?

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