Solthall
Member
Hello.
Got some weird issue with my collision. I'm aware that this might not be the best code, but I'm still learning.
Hard to find complete tutorials on top down movement and collisions (taking sprite/mask rotations into account). But managed to draw the player sprites in the draw event, even though it's quite a hassle.
So the collision works from three angles, when I hit the solid from the top, beneath and the right. Left is not working, player walks into the wall and gets stuck!
The game is top down, player moves towards and rotates with mouse angle.
Here's the whole movement script:
Got some weird issue with my collision. I'm aware that this might not be the best code, but I'm still learning.
Hard to find complete tutorials on top down movement and collisions (taking sprite/mask rotations into account). But managed to draw the player sprites in the draw event, even though it's quite a hassle.
So the collision works from three angles, when I hit the solid from the top, beneath and the right. Left is not working, player walks into the wall and gets stuck!
The game is top down, player moves towards and rotates with mouse angle.
Here's the whole movement script:
GML:
function scr_movement()
{
//Input movement
forward = keyboard_check(ord("W"));
backward = keyboard_check(ord("S"));
right = keyboard_check(ord("D"));
left = keyboard_check(ord("A"));
//Movement direction
if obj_stairs.alarm[0] == -1
{
if forward
{
speed = walkSpeed;
}
if backward
{
speed = -walkSpeed;
}
if right
{
direction += 90;
speed = -walkSpeed;
}
if left
{
direction -= 90;
speed = -walkSpeed;
}
}
// Stop moving when no key is pressed // Not working, I can keep moving if I press any key while pressing movement input
if keyboard_check(vk_nokey)
{
speed = 0;
image_index = 0;
//image_speed = 0;
}
// Stop moving if multiple inputs detected
if left and right
{
speed = 0;
image_index = 0;
image_speed = 0;
}
// Stop moving if colliding with mouse pointer or shop/inventory open
if point_distance(x,y,mouse_x,mouse_y) < 15
or global.shop == true
or obj_inv_control.drawInventory == true
{
speed = 0;
image_index = 0;
image_speed = 0;
}
if instance_exists(obj_attack) and obj_attack.image_index <= 2 //Player attack speed boost
{
speed += 2;
}
//Collision check
if place_meeting(x + hspeed, y, obj_solid)
{
if !place_meeting(x + sign(hspeed), y, obj_solid) //while (instead of "if" but causes crash)
{
speed -= min(abs(hspeed),1)*sign(hspeed);
}
hspeed = 0;
}
if place_meeting(x, y + vspeed, obj_solid)
{
if !place_meeting(x, y + sign(vspeed), obj_solid) //while (instead of "if" but causes crash)
{
speed -= min(abs(vspeed),1)*sign(vspeed);
}
vspeed = 0;
}
}