GML Player can no longer move after adding PlayerCollision script.

E

EAE01120

Guest
Hey all! I am following a Shaun Spalding tutorial on YouTube, and I have came across the same issue I did on another project following the same video. The moment I add a collision script, my character is stuck in place even when it is not touching something it can collide with. Here is my Player Step Code-

GML:
//MOVE AND ANIMATION START
keyLeft = keyboard_check(vk_left);
keyRight = keyboard_check(vk_right);
keyUp = keyboard_check(vk_up);
keyDown = keyboard_check(vk_down);
keyUse = keyboard_check_pressed(vk_space);

inputDirection = point_direction(0,0,keyRight-keyLeft,keyDown-keyUp);
inputMagnitude = (keyRight - keyLeft != 0) || (keyDown - keyUp != 0);

hSpeed = lengthdir_x(inputMagnitude * speedWalk, inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedWalk, inputDirection);


PlayerCollision();

var _oldSprite = sprite_index;
if (inputMagnitude != 0)
{
    direction = inputDirection
    sprite_index = spriteRun;
}    else sprite_index = spriteIdle;
if (_oldSprite != sprite_index) localFrame = 0;

playerAnimateSprite();
//END
And here is my PlayerCollision script-

Code:
function PlayerCollision(){
    
var _collision = false;

if (tilemap_get_at_pixel(collisionMap, x + hSpeed, y))
{
    x -= x mod TILE_SIZE;
    if (sign(hSpeed) == 1) x += TILE_SIZE - 1;
    hSpeed = 0;
    _collision = true;
}
x += hSpeed;

if (tilemap_get_at_pixel(collisionMap, x , y + vSpeed))
{
    y -= y mod TILE_SIZE;
    if (sign(vSpeed) == 1) y += TILE_SIZE - 1;
    vSpeed = 0;
    _collision = true;
}
y += vSpeed;

It is currently 2 in the morning where I am so I will get to what responses I can in the morning. Thanks for your help! 



return _collision;
}
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
What I think could be the issue is this line:
GML:
if (tilemap_get_at_pixel(collisionMap, x + hSpeed, y))
tilemap_get_at_pixel returns an integer value, NOT a boolean value, and when I use tilemaps for collisions I always check for the index of the tile that is being used in the collision. So, how is the collision tilemap setup and what is the index of the tile you're using for the collision check?
 
E

EAE01120

Guest
What I think could be the issue is this line:
GML:
if (tilemap_get_at_pixel(collisionMap, x + hSpeed, y))
tilemap_get_at_pixel returns an integer value, NOT a boolean value, and when I use tilemaps for collisions I always check for the index of the tile that is being used in the collision. So, how is the collision tilemap setup and what is the index of the tile you're using for the collision check?
Thanks for your reply! This is what is in the create for my Player obj regaurding the collisionMap.
GML:
collisionMap = layer_tilemap_get_id(layer_get_id("Col"));
Col is my Tileset layer in my room. Thanks again
 
E

EAE01120

Guest
Man, I've been working at this all day to figure it out and still no luck. I've gone back over every code and they all match what it is supposed to be. The collisions worked at first, but then when I started the game initialization and pausing video my character got stuck. I must've messed something up somewhere. Would love to get this fixed
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
So, what I meant by the index of the tiles, was not the index of the tile LAYER, but the index of the tile on the layer. By default, an "empty" tile is index 0, and all other tiles are numbered from 1. So, if your tile layer has no empty tiles and is using - for example - tile index 3 for collisions, then the code above won't work. It will only work if the layer being checked has empty tiles, OR if you specify a specific tile index to check if no empty tiles are present.
 
E

EAE01120

Guest
So, what I meant by the index of the tiles, was not the index of the tile LAYER, but the index of the tile on the layer. By default, an "empty" tile is index 0, and all other tiles are numbered from 1. So, if your tile layer has no empty tiles and is using - for example - tile index 3 for collisions, then the code above won't work. It will only work if the layer being checked has empty tiles, OR if you specify a specific tile index to check if no empty tiles are present.
AH! That worked.. what I did was delete the Col layer in my room, then just remade it. Whatever issue I had caused in the room with empty tiles or what have you is now gone. Thanks so much!
 
Top