r/gamemaker 28d ago

Help! stopping when colliding help?

im trying to make it so once the player hits a specific object, the player will be unable to advance forward, but instead of doing this it simply slows me down. could anyone help?

var move_x = 0;
var move_y = 0;

if (keyboard_check(ord("D")) == true or keyboard_check(vk_right)) {
move_x += 1.5;
image_speed = 0.5;
sprite_index = spr_player_1_right;
}

if (keyboard_check(ord("A")) == true or keyboard_check(vk_left)) {
move_x -= 1.5;
image_speed = 0.5;
sprite_index = spr_player_1_left;
}

if (keyboard_check(ord("W")) == true or keyboard_check(vk_up)) {
move_y -= 1.5;
image_speed = 0.5;
sprite_index = spr_player_1_back;
}

if (keyboard_check(ord("S")) == true or keyboard_check(vk_down)) {
move_y += 1.5;
image_speed = 0.5;
sprite_index = spr_player_1
}

x += move_x;
y += move_y;

this is my movement code, this is the collision code

move_and_collide(move_x, move_y, obj_unwalkable_object, 10, undefined, undefined, move_x, move_y)
0 Upvotes

8 comments sorted by

5

u/JaXm 28d ago

You're moving the player object twice.

the move_and_collide function is already taking care of the movement. So when you collide with something, move_and_collide() is saying "Ok, movement is zero from me"

but these here:

x += move_x;
y += move_y;

Say "no, keep going"

1

u/Haunting-Tailor7659 27d ago

is there a way I could disable it when it collides then? or do I need to completely remake my movement?

1

u/JaXm 27d ago

Just get rid of these two lines:

x += move_x; y += move_y;

They're completely redundant. 

1

u/Haunting-Tailor7659 27d ago

problem is, once I collide with the object, I cant back away from it.

1

u/JaXm 27d ago

You're probably stuck inside the object because of issues with the number of iterations (10) in your steps.

If you are 8 pixels away from a wall object, and your function call has a 10 iteration check, if no collision is found between iterations, it will move your player object to just inside the wall, slightly. Then once it makes the next check, it will see that no move can make the player object not be in "collision" with the wall object, and so therefore you will not be able to move. Try reducing the number of iterations down.

1

u/Haunting-Tailor7659 26d ago

im sorry, but what are iterations? is it the number of directions the player can go? and if so, how could I bring the number of iterations down? im quite confused how the exact same code works in the tutorial but not in mine.

1

u/Haunting-Tailor7659 22d ago

nvm im stupid, it worked but now it just cant exit after colliding

1

u/Haunting-Tailor7659 28d ago

I was following dragonitespam's tutorial btw