Z
ZeroTheScyther
Guest
Hello there. I'm trying to make an platformer where this square would move on it's own to one direction then change direction when it touches an wall. Right now for some reason, it won't update when touching an wall.
That's the code I've got off some youtube video and it's to make the square stop when it hits an wall. That totally works but if I change it like
nothing happens, even tho if I add a send message, it returns that "move" changed to -1. Trying to forcefully set hsp to -4 has a weird effect and the square moves back and forth 4 pixels once it hits the wall and keeps smacking against the wall.
So, anyone can explain what should I do to make my square change direction once it collides with an wall?
///GetPlayerInput
key_right = keyboard_check(ord("D"));
key_left = -keyboard_check(ord("A"));
key_jump = keyboard_check_pressed(vk_space);
//ReactToInputs
move = 1;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;
if (place_meeting(x,y+1, Groundio))
{
vsp = key_jump * -jumpspeed
}
//HorizonatCollision
if (place_meeting(x+hsp, y, Wallio))
{
while(!place_meeting(x+sign(vsp), y, Wallio))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
//VerticalCollision
if (place_meeting(x, y+vsp, Groundio))
{
while(!place_meeting(x, y+sign(vsp), Groundio))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
key_right = keyboard_check(ord("D"));
key_left = -keyboard_check(ord("A"));
key_jump = keyboard_check_pressed(vk_space);
//ReactToInputs
move = 1;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;
if (place_meeting(x,y+1, Groundio))
{
vsp = key_jump * -jumpspeed
}
//HorizonatCollision
if (place_meeting(x+hsp, y, Wallio))
{
while(!place_meeting(x+sign(vsp), y, Wallio))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
//VerticalCollision
if (place_meeting(x, y+vsp, Groundio))
{
while(!place_meeting(x, y+sign(vsp), Groundio))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
That's the code I've got off some youtube video and it's to make the square stop when it hits an wall. That totally works but if I change it like
if (place_meeting(x+hsp, y, Wallio))
{
while(!place_meeting(x+sign(vsp), y, Wallio))
{
x += sign(hsp);
}
move = move * -1;
}
{
while(!place_meeting(x+sign(vsp), y, Wallio))
{
x += sign(hsp);
}
move = move * -1;
}
So, anyone can explain what should I do to make my square change direction once it collides with an wall?