Help with diagonal movement

P

piksil_demon

Guest
hey guys, so heres my problem. I used this code before and it worked perfectly, but now for some reason my character moves to the right without intervention. What went wrong? How do i fix it?

Code:
up = keyboard_check(ord('W'))
down = keyboard_check(ord('S'))
left = keyboard_check(ord('A'))
right = keyboard_check(ord('D'))

var Hspd= (right - left);
var Vspd= (down - up);
var d = point_direction(0, 0, Hspd, Vspd);

x += lengthdir_x(Spd, d);
y += lengthdir_y(Spd, d);
 
P

piksil_demon

Guest
You're not setting Spd to 0 when the player stopped moving.
lol, i feel silly now. to anyone else having this problem please use the following code-

Code:
up = keyboard_check(ord('W'))
down = keyboard_check(ord('S'))
left = keyboard_check(ord('A'))
right = keyboard_check(ord('D')

//not moving or holding opposit keys ("5" can be changed to suit your liking)
if!left & !right & !up &! down or up & down or left & right
{Spd=0}
else{Spd = 5}

//movement code
var Hspd= (right - left)
var Vspd= (down - up);

//making diagonals the same speed as up, down, left, right
var d = point_direction(0, 0, Hspd, Vspd);
x += lengthdir_x(Spd, d); 
y += lengthdir_y(Spd, d);
 
Last edited by a moderator:
Top