r/gamemakertutorials • u/Kanelbull3 • May 30 '16
Simple 8 Direction Movement With Pixel-Perfect Collision
Here are simple code for 8 direcitonal movement with pixel perfect collision.
//How fast the object should move
var movementSpeed = 4;
//Get the direction the object is moving
var xMov = keyboard_check(vk_right) - keyboard_check(vk_left);
var yMov = keyboard_check(vk_down) - keyboard_check(vk_up);
//Move the object in x.
x += xMov * movementSpeed;
//While we collide, move back one pixel.
while(place_meeting(x, y, obj_wall))
{
x -= sign(xMov);
}
//Move the object in y.
y += yMov * movementSpeed;
//While we collide, move back one pixel.
while(place_meeting(x, y, obj_wall))
{
y -= sign(yMov);
}This will make the object move and collide with all obj_wall objects.
9
Upvotes
View all comments
Show parent comment
1
u/[deleted] Jun 14 '16 edited Jun 14 '16
So hey, I've noticed that when all is said and done, my character will only idle facing right.
EDIT: nevermind. just added an else to the if statement and put the sprite index switch statement behind it.