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

Pushing box problem(works one way not other)

J

Jaxon

Guest
I'm having a problem with my gml script. the objective is to when my sprite walks into the cube it moves in the same direction. but my cube travels through walls and only works when moving right, pushing the cube left results into it moving to the other side of the sprite(left to right).
My code:
create event:
[
hsp = 0;
vsp = 0;
grv = 0.3;
]
step event:
[
//Gravity
vsp = vsp + grv;
//Vertical Collision
if (place_meeting(x,y + vsp,obj_wall))
{
while (!place_meeting(x,y + sign(vsp),obj_wall))
{
y = y + sign(vsp)
}
vsp = 0;
}
y = y + vsp;

//Horizontal Collision
if (place_meeting(x + hsp,y,obj_wall))
{
while (!place_meeting(x + sign(hsp),y,obj_wall))
{
x = x + sign(hsp)
}
hsp = 0;
}
x = x + hsp;

//Push block
//moving right
if (place_meeting(x+1,y,obj_player))
{
while (place_meeting(x+1,y,obj_player))
{
x += 0.01;
}
}
//moving left
if (place_meeting(x-1,y,obj_player))
{
while (place_meeting(x+1,y,obj_player))
{
x -= 0.01;
}
}
]
Help would be greatly appreciated
 

3dgeminis

Member
Try this way:
Code:
//Move Box
with(instance_place(x+hsp,y,obj_box)) { if !place_meeting(x+other.hsp,y,obj_wall) {x+=other.hsp} }

//Gravity
vsp = vsp + grv;
//Vertical Collision
if (place_meeting(x,y + vsp,obj_wall))
    {
     while (!place_meeting(x,y + sign(vsp),obj_wall)) {y = y + sign(vsp)}
     vsp = 0;
    }
y = y + vsp;

//Horizontal Collision
if (place_meeting(x + hsp,y,obj_wall))
    {
     while (!place_meeting(x + sign(hsp),y,obj_wall)){x = x + sign(hsp)}
     hsp = 0;
    }
x = x + hsp;
Also you have to parent the box to the object wall, that way the box does not trespass the wall.
 
J

Jaxon

Guest
Try this way:
Code:
//Move Box
with(instance_place(x+hsp,y,obj_box)) { if !place_meeting(x+other.hsp,y,obj_wall) {x+=other.hsp} }

//Gravity
vsp = vsp + grv;
//Vertical Collision
if (place_meeting(x,y + vsp,obj_wall))
    {
     while (!place_meeting(x,y + sign(vsp),obj_wall)) {y = y + sign(vsp)}
     vsp = 0;
    }
y = y + vsp;

//Horizontal Collision
if (place_meeting(x + hsp,y,obj_wall))
    {
     while (!place_meeting(x + sign(hsp),y,obj_wall)){x = x + sign(hsp)}
     hsp = 0;
    }
x = x + hsp;
Also you have to parent the box to the object wall, that way the box does not trespass the wall.
sorry but it doesn't work(and yes I replaced the obj_box with my box obj name) but it acts as a collision, the player stops moving when it touches the box and can stand on it but the box doesn't move when pushed. MY EDIT OF THIS sorry but what i did was put the move box code in the box so the box was moving the player without ever moving, i put it in my player code and it worked perfectly thx for the big help ;)
 
Last edited by a moderator:
Top