• 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 [SOLVED](2D) (Platformer) Random movement in 2 directions

Hi
What's the code for when the object hits the obj_wall object, that it will chose to either walk left or right?

if instance_place(x,y+1,obj_wall) {
hspeed = -1.2 OR 1.2
}

My idea is like that, but what's the code for it
 
O

orSQUADstra

Guest
thanks - now i just have to solve the fact that they constantly walk back and forth instead of just choosing one direction and sticking with it
I assume you use vspeed. this code will apply to both y+1 and y-1 so you don't have to have this code twice. This code will only change the hspeed if it was previously 0. That should fix your problem. (that is, if your object doesn't go diagonally.)
Code:
if (instance_place(x,y+sign(vspeed),obj_wall) && hspeed = 0) {
hspeed = choose(-1.2,1.2);
}
 
I assume you use vspeed. this code will apply to both y+1 and y-1 so you don't have to have this code twice. This code will only change the hspeed if it was previously 0. That should fix your problem. (that is, if your object doesn't go diagonally.)
Code:
if (instance_place(x,y+sign(vspeed),obj_wall) && hspeed = 0) {
hspeed = choose(-1.2,1.2);
}
Why do we use sign and check for vspeed?
 
O

orSQUADstra

Guest
Why do we use sign and check for vspeed?
Actually you can leave that y+1 instead of y+sign(vspeed), it really depends on what kind of game you have. If it's a platformer then y+1 is good, if it's a top down game then you can use y+sign(vspeed) if you want this condition to apply to both y+1 and y-1, since sign will return either -1, 0 or 1 depending on the value of vspeed. If it's a side view platformer then you don't want that obviously (except if the enemy can also move on ceilings)
 
Top