• 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]keep same direction when colliding with wall

FoufaDjo

Member
when the object move to the left and collide with the wall he stop moving but change is direction to 0 how can i fix that
spdmax = 2;
accel = 0.5;
friction = 0.3;
if rkey hspeed += accel;
if lkey hspeed -= accel;
if dkey vspeed += accel;
if ukey vspeed -= accel;
speed = clamp(speed,-spdmax,spdmax);
if (place_meeting(x+hspeed, y, osolidobjects)) {//Horizontal Collision
while(!place_meeting(x+sign(hspeed), y, osolidobjects))
x += sign(hspeed);
hspeed = 0;
}
if (place_meeting(x, y+vspeed, osolidobjects)) {// Vertical Collision
while(!place_meeting(x, y+sign(vspeed), osolidobjects))
y += sign(vspeed);
vspeed = 0;
}
 

TsukaYuriko

☄️
Forum Staff
Moderator
direction automatically becomes 0 when speed becomes 0. There is no way to change this behavior.

To work around it, you could keep a temporary copy of direction in memory which only updates its value to direction if it isn't 0. That way, you'll always have access to the latest value of direction that isn't 0.

If 0 is a valid value for your usage case of it, refer to the temporary copy if and only if speed is 0, otherwise, refer to direction itself.


... or program movement manually so you have full control over it and don't have to rely on workarounds.
 

FoufaDjo

Member
it worked i put if speed != 0 dir = direction before collision then but direction = dir after thnx bro for the help uwu
knockback = max(knockback-2,0);
if(knockbackon = 1) {
direction = knockbackdir;
speed = 6.5;
if(knockback = 0) {
speed = 0;
direction = dir;
knockbackon = 0;
}
} else {
if rkey hspeed += accel;
if lkey hspeed -= accel;
if dkey vspeed += accel;
if ukey vspeed -= accel;
speed = clamp(speed,-spdmax,spdmax);
if speed != 0 dir = direction;
if (place_meeting(x+hspeed, y, osolidobjects)) {
while(!place_meeting(x+sign(hspeed), y, osolidobjects))
x += sign(hspeed);
hspeed = 0;
}
if (place_meeting(x, y+vspeed, osolidobjects)) {
while(!place_meeting(x, y+sign(vspeed), osolidobjects))
y += sign(vspeed);
vspeed = 0;
}
if(knockback > 0){
dir = direction;
knockbackon = 1;
}
}
 
Top