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

GameMaker Help! PlayerCollision only working on left side, but not right side in sidescroller platformer

D

DarkCaelestis

Guest
UPDATE: Someone helped me resolve the problem on reddit, it all works now. :)

Hello! So I'm an artist and I've turned to GM2 to start developing my own game, but I don't know how to code. So far, I've been using tutorials to help me learn, so please be patient with me if I find it difficult to understand some answers. I've used Shaun Spalding's tutorial on collisions, but I'm having glitches when I collide on the right hand side--the left side works perfectly. When I have only horizontal collision on, my character runs through most of the right hand tile before stopping. When I added the vertical collision, it then makes my character teleport several tiles upward upon impact and the character will disappear if I make it go offscreen on the right hand side. Left hand side still works perfectly with both horizontal and vertical collision on.

My tiles are 16x16 and his tutorial is for 32x32 tiles, but I'm wondering if maybe that has something to do with the trouble I am having. I did change the tile size in the sections of the code he said were meant for that, and I also went back and changed some layers in my room so that the grid was 16x16 instead of 32x32. I did notice that some layers weren't changed before so I thought it would fix the problem, but it honestly made no effect on the collision working.

I played around with my character's origin point quite a bit too, which sometimes affected whether or not the left hand side collision would work. My current origin point is 7,16, which is one pixel below the character (otherwise she intercepts with the tiles below). She is also 16x16 in size.

Here is my current code for the player character:

var bbox_side;

key_right = keyboard_check ("D");
key_left = keyboard_check ("A");
key_up = keyboard_check ("W");
key_down = keyboard_check ("S");

if(keyboard_check(ord("D"))) {
x += walkSpeed= 7;
image_speed = walkSpeed /3;
sprite_index = HealerRunRight;
rightmove = true;
leftmove = false;
}

if(keyboard_check(ord("A"))) {
x -= walkSpeed= 7;
image_speed = walkSpeed /3;
sprite_index = HealerRunLeft;
leftmove = true;
rightmove = false;
}
if(keyboard_check(vk_nokey)) and (rightmove) {
image_speed = 0;
sprite_index = HealerIdleRight;
walkspeed = 3.5;
}

if(keyboard_check(vk_nokey)) and (leftmove) {
image_speed = 0;
sprite_index = HealerIdleLeft;
walkspeed = 3.5;
}

//Floor and Slope Collision

hsp = (key_right - key_left) * 4;
vsp = (key_down - key_up) * 4;

//Horizontal Collision
if (hsp > 0) bbox_side = bbox_right; else bbox_side = bbox_left;
if (tilemap_get_at_pixel(tilemap,bbox_side+hsp,bbox_top) != 0) or
(tilemap_get_at_pixel(tilemap,bbox_side+hsp,bbox_bottom) != 0)
{
if (hsp > 0) x = x - (x mod 16) + 15 - (bbox_right - x);
else x = x - (x mod 16) - (bbox_left - x);
hsp = 0;
}
x += hsp;

//Vertical Collision

if (vsp > 0) bbox_side = bbox_bottom; else bbox_side = bbox_top;
if (tilemap_get_at_pixel(tilemap,bbox_left,bbox_side+vsp) != 0) or
(tilemap_get_at_pixel(tilemap,bbox_right,bbox_side+vsp) != 0)
{
if (vsp > 0) y = y - (y mod 16) + 15 - (bbox_right - y);
else y = y - (y mod 16) - (bbox_left - y);
vsp = 0;
}
y += vsp;

Thanks so much in advance!
 
Last edited by a moderator:
Top