• 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!

When something works and you don't know why GM2

R

Ragedleinad

Guest
Hi team, well I have this object that is a enemy, for now I just want it to move left and right , each time its one pixel away from a wall it will invert it's velocity to have a negative value and viceversa

On the Create event I have:
Code:
objXVelocity = 5;
On the Step event I have :
Code:
/*When hitting right wall I go other direction*/
if (place_meeting(x+1,y,obj_Floor)){
       objXVelocity = objXVelocity * (-1);
}
x= x + objXVelocity;

Should I have the same place_meeting but for left collition? if (place_meeting(x-1,y,obj_Floor)){

I made a gif -> https://gph.is/2H9itDZ
 

Bentley

Member
Instead of two checks, you could use 1 with "sign". Ex:
Code:
if (place_meeting(x + sign(hspd), y, obj_wall))
{
   hspd = -hspd;
}
 
R

Ragedleinad

Guest
Instead of two checks, you could use 1 with "sign". Ex:
Code:
if (place_meeting(x + sign(hspd), y, obj_wall))
{
   hspd = -hspd;
}
Nice approach,will use,but i still dont know why my version is working
 

Bentley

Member
You're version works because you do go into the left wall, eventually making this condition true: if (place_meeting(x + 1, y, obj_wall))
 
R

Ragedleinad

Guest
You're version works because you do go into the left wall, eventually making this condition true: if (place_meeting(x + 1, y, obj_wall))
But checking left wall would be place_meeting(x-1,y,obj_wall)?
 
But checking left wall would be place_meeting(x-1,y,obj_wall)?
You check only one pixel ahead to the right but you move at a rate of 5 pixels in either direction. If a wall is even 1 pixel away from the left, the code will, of course, still find nothing and then move 4 pixels into the wall. Now, if you check x+1, there will indeed be a wall at that position, reversing the direction.
 
R

Ragedleinad

Guest
You check only one pixel ahead to the right but you move at a rate of 5 pixels in either direction. If a wall is even 1 pixel away from the left, the code will, of course, still find nothing and then move 4 pixels into the wall. Now, if you check x+1, there will indeed be a wall at that position, reversing the direction.
Sorry for late reply, I get it now, thanks :)
 
Top