Object gets stuck, on walls.

Hi! I've been learning about coding and I modified some code for collisions that I got from a tutorial so that I could use it as a script. It worked at first but suddenly, something changed and now My object gets stuck in walls and my tile collision layer.

GML:
function playerMove(){
if(instance_exists(obj_cursor))
{
moveX = lengthdir_x(spd, dir);
moveY = lengthdir_y(spd, dir);

collision();

x += moveX;
y += moveY;
}
else
{
moveX = 0;
moveY = 0;
}

}
function collision(){
///Check X Collisions
if(tileCollide(x + moveX, y, "col"))
    {
    while(!tileCollide(x + sign(moveX), y, "col"))
    {x += sign(moveX)}
    
    moveX = 0;
    moveY = 0;
    
    }

if(place_meeting(x + moveX, y, par_wall))
    {
    while(!place_meeting(x + sign(moveX), y, par_wall))
    {x += sign(moveX)}
    
    moveX = 0;
    moveY = 0;
    
    }

///Check Y Collisions
if(tileCollide(x, y + moveX, "col"))
    {
    while(!tileCollide(x, y + sign(moveX), "col"))
    {y += sign(moveX)}
    
    moveX = 0;
    moveY = 0;
    
    }
    
if(place_meeting(x, y + moveX, par_wall))
    {
    while(!place_meeting(x, y + sign(moveX), par_wall))
    {y += sign(moveX)}
    
    moveX = 0;
    moveY = 0;
    
    }
}
function tileCollide(){
var xx = argument0;
var yy = argument1;
var layer_id = layer_tilemap_get_id(layer_get_id(argument2));

var xp = x;
var yp = y;

x = xx;
y = yy;

var meeting =
    tilemap_get_at_pixel(layer_id, bbox_left, bbox_top) ||
    tilemap_get_at_pixel(layer_id, bbox_right, bbox_top) ||
    tilemap_get_at_pixel(layer_id, bbox_left, y - 15) ||
    tilemap_get_at_pixel(layer_id, bbox_right, y - 15) ||
    tilemap_get_at_pixel(layer_id, bbox_left, bbox_bottom) ||
    tilemap_get_at_pixel(layer_id, bbox_right, bbox_bottom) ||
    tilemap_get_at_pixel(layer_id, x, y - 15);

x = xp;
y = yp;

return meeting;
}
any insight will be greatly appreciated!
 

Nidoking

Member
y + moveX
You've got this all over your collision functions. Why are you checking for vertical collisions using horizontal speed? And why do you halt ALL movement based on either a horizontal or vertical collision. Surely you only want to stop moving in the direction of the collision. I think you need to redo that tutorial.
 
You've got this all over your collision functions. Why are you checking for vertical collisions using horizontal speed? And why do you halt ALL movement based on either a horizontal or vertical collision. Surely you only want to stop moving in the direction of the collision. I think you need to redo that tutorial.
I think you may be right.

I fixed what you pointed out and it works flawlessly now. It's always something simple like that. I also know exactly when the mess-up happened. I was modifying it and just to be lazy, I did some copy paste to duplicate some code and forgot to update the variables. As for stopping all motion on a vertical or horizontal collision, that was a matter of seeing the motion stop as part of the Cursor Object check and forgetting why that arrests all motion and just repeating it down the line.

Thanks A lot! it was such a small issue that I'm sure I would have been racking my brain for days about it.
 
Top