Pushing and pulling an object by the player

sensodyne

Member
Hello everyone I used a few tutorials and made myself an object that the player can push.

I only miss two things to do.

1.the Hero pushes the object to the end of the room and then he should not push it any further .. both when it is on the left side of the room or the right side of the room
2.The hero should also be able to pull an object towards him and pull it

how to do it? My code looks like this

obj_chest

create

DnD blocks

GML:
Set variable
Applies to
Self

Variable uboot
0

GML:
Set variable
Applies to
Self

Variable bul
0

Begin Step

GML:
///the moving box can be moved left and  right
if place_meeting(x,y,obj_water)
{
    
    uboot=1;
    
}
else
{
    uboot=0;
}




//gravitation
if (place_free(x,y+2) && !place_meeting(x,y+10,obj_hero))
{
    switch (uboot)
    {
        case 0:{gravity = 0.5;break;}
        case 1:{gravity = 0.01;break;}
    }
    
}
else {gravity = 0;}

gravity_direction = 270
///pushing the box by the player
if place_meeting(x-5,y,obj_hero) && keyboard_check(key.right) && place_free(x+2,y)
{
x+=2;
}
if place_meeting(x+5,y,obj_hero) && keyboard_check(key.left) && place_free(x-2,y)
{
x-=2;
}

collision with obj_ground, i.e. hard ground


GML:
if vspeed > 0 && !place_free(x,y+vspeed) {move_contact(270)}
vspeed = 0
gravity = 0

collision with obj_hero


GML:
if vspeed > 0 && !place_free(x,y+vspeed) {move_contact(270)}
vspeed = 0
gravity = 0


obj_hero

create

GML:
subtract = 0;

End Step

GML:
 //animation for moving the chest by the hero
 if in_AIR == false && (keyboard_check(key.right)) && (place_meeting(x-5,y,obj_chest) || place_meeting(x+5,y,obj_chest))
 {sprite_index = spr_hero_moves_the_object; image_index = 23 -(subtract);
  subtract -= 1; }
  else if (keyboard_check(key.left)) && (place_meeting(x-5,y,obj_chest) || place_meeting(x+5,y,obj_chest)) {
  sprite_index = spr_hero_moves_the_object; image_index = 23 -(subtract);
  subtract -= 1; }

Thank you very much for any help
 

sensodyne

Member
I care more about setting a border point on the room scenery on the left and right side of the room to which the player can push the object to the maximum.
Let's say it is 100px on the left side of the room and 100px on the right side of the room.

as described in the picture below then I would prevent the object from jamming


push object room.png

my code works fine..I made it as best I could. but I miss this option to prevent jamming object and Hero sprite. That's why this distance is i need

Thank you so much for any help :)
 

sensodyne

Member
Ok i solved it like this.. i added such a line to the code i used clamp function..and now the item stops 100px from the room boundary on the left and right side

GML:
x = clamp(x, 100, room_width-100);

I have a question. I have an object called an obstacle ... and I would also like to set limits there to which point the player can push the sliding object.
let it also be 100px on the left and right side of the object obj_obstacle
 
Top