• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker How to move object randomly?

Hello all, I need some help as i'm relatively new to GMS2.

I have a room 640x360, I have an object. I would like for that object to move randomly in the room. With the code I have, it's only moving left and right. Any guidance would be greatly appreciated. The code that I have currently is as below:

switch (state) {

#region Move Right

case slime.move_right:

var wall_at_right = place_meeting(x + 1, y, o_solid);

if (wall_at_right) {

state = slime.move_left;

}

//Direction of image

image_xscale = -1;

//Speed of image moving

x += 1;



break;





#endregion



#region Move Left

case slime.move_left:

var wall_at_left = place_meeting(x - 1, y, o_solid);

if (wall_at_left) {

state = slime.move_right;

}

//Direction of image

image_xscale = 1;

//Speed of image moving

x -= 1;

break;



#endregion

}
 
Ç

Çağrı

Guest
if z = 80
{
a = random_range(-3,3)
b = random_range(-30,30)
}


if z <= 80 z += 1
else z = 0
if z > 50 mp_potential_step(x + a ,y + b , 2 , true)

It might have flaws and be inefficent but this is how it works:
put this in the step event and it counts up to 80 waits for 50 frames and moves for 30 frames
when it reaches 80 it randomizes the points again then waits for 50 frames
 
if z = 80
{
a = random_range(-3,3)
b = random_range(-30,30)
}


if z <= 80 z += 1
else z = 0
if z > 50 mp_potential_step(x + a ,y + b , 2 , true)

It might have flaws and be inefficent but this is how it works:
put this in the step event and it counts up to 80 waits for 50 frames and moves for 30 frames
when it reaches 80 it randomizes the points again then waits for 50 frames


Thank you very much.
 
Top