amelianreasons
Member
I'm new to GameMaker 2 and I'm having trouble with collision/walls. I'm able to make my character walk around, but when I touch side walls, I can only move away from the wall, not up or down. I don't have the same issue with the bottom and top walls.
Here is my code:
If my character is against a vertical object (that I'm using for a wall) like this, then she can only walk away from the wall (to the right, not up or down).
However, if my character is against a horizontal object, then she can walk left/right, and up:
Unsure of why it's doing this. Any help is appreciated. Thanks!
Here is my code:
GML:
walk_speed = 3;
if (keyboard_check(vk_up))
{
sprite_index = walk_back;
// If there is not a wall walk_speed away
if (!place_meeting(x, y - walk_speed, oWall))
{
// Move walk_speed
y -= walk_speed;
}
// Otherwise, there is a wall walk_speed away
else
{
// So move 1 pixel up until you are next to the wall
while (!place_meeting(x, y - walk_speed, oWall))
{
y--;
}
}
}
if (keyboard_check(vk_left))
{
sprite_index = walk_left;
// If there is not a wall walk_speed away
if (!place_meeting(x - walk_speed, y, oWall))
{
// Move walk_speed
x -= walk_speed;
}
// Otherwise, there is a wall walk_speed away
else
{
// So move 1 pixel up until you are next to the wall
while (!place_meeting(x - walk_speed, y, oWall))
{
x--;
}
}
}
if (keyboard_check(vk_right))
{
sprite_index = walk_right;
// If there is not a wall walk_speed away
if (!place_meeting(x + walk_speed, y, oWall))
{
// Move walk_speed
x += walk_speed;
}
// Otherwise, there is a wall walk_speed away
else
{
// So move 1 pixel up until you are next to the wall
while (!place_meeting(x + walk_speed, y, oWall))
{
x++;
}
}
}
if (keyboard_check(vk_down))
{
sprite_index = walk_front;
// If there is not a wall walk_speed away
if (!place_meeting(x, y + walk_speed, oWall))
{
// Move walk_speed
y += walk_speed;
}
// Otherwise, there is a wall walk_speed away
else
{
// So move 1 pixel up until you are next to the wall
while (!place_meeting(x, y + walk_speed, oWall))
{
y++;
}
}
}
if(keyboard_check_released(vk_down)){
sprite_index = front_idle;
}
if(keyboard_check_released(vk_up)){
sprite_index = back_idle;
}
if(keyboard_check_released(vk_left)){
sprite_index = left_idle;
}
if(keyboard_check_released(vk_right)){
sprite_index = right_idle;
}
However, if my character is against a horizontal object, then she can walk left/right, and up:
Unsure of why it's doing this. Any help is appreciated. Thanks!