GameMaker How to move relative to the mouse

G

gamer_essence

Guest
So i have a character that moves but i want to move him in correspondance with the mouse. I.e. if you press w it moves toward the mouse and s it goes backwards. I also want to have it move left and right in correspondance with the mouse as well. I've tried for hours to figure this out and anything i do doesnt work so any help would be appreciated.
 
T

TinCan

Guest
Just get the angle to the mouse and then use trig
Code:
angle = point_direction(x, y, mouse_x, mouse_y);
max_speed = 5;

if(keyboard_check(ord("W"))){
     x += max_speed * dcos(angle);
     y += max_speed * dsin(angle);
}
if(keyboard_check(ord("S"))){
     x -= max_speed * dcos(angle);
     y -= max_speed * dsin(angle);
}
 
J

JFitch

Guest
Just get the angle to the mouse and then use trig
Code:
angle = point_direction(x, y, mouse_x, mouse_y);
max_speed = 5;

if(keyboard_check(ord("W"))){
     x += max_speed * dcos(angle);
     y += max_speed * dsin(angle);
}
if(keyboard_check(ord("S"))){
     x -= max_speed * dcos(angle);
     y -= max_speed * dsin(angle);
}
Since Game Maker uses up as -y and down as +y, it may actually be this:
Code:
angle = point_direction(x, y, mouse_x, mouse_y);
max_speed = 5;

if(keyboard_check(ord("W"))){
     x += max_speed * dcos(angle);
     y -= max_speed * dsin(angle);
}
if(keyboard_check(ord("S"))){
     x -= max_speed * dcos(angle);
     y += max_speed * dsin(angle);
}
 
G

gamer_essence

Guest
Since Game Maker uses up as -y and down as +y, it may actually be this:
Code:
angle = point_direction(x, y, mouse_x, mouse_y);
max_speed = 5;

if(keyboard_check(ord("W"))){
     x += max_speed * dcos(angle);
     y -= max_speed * dsin(angle);
}
if(keyboard_check(ord("S"))){
     x -= max_speed * dcos(angle);
     y += max_speed * dsin(angle);
}
well this solves the forward and backward problem but what about left and right? It might be simple but i can not figure out how. The only way that i can think of is making it move 90 degrees from that direction but i dont know how
 
J

JFitch

Guest
Code:
angle = point_direction(x, y, mouse_x, mouse_y);
max_speed = 5;

if(keyboard_check(ord("W"))){
    x += max_speed * dcos(angle);
    y -= max_speed * dsin(angle);
}
if(keyboard_check(ord("S"))){
    x -= max_speed * dcos(angle);
    y += max_speed * dsin(angle);
}
if(keyboard_check(ord("A"))){
    x -= max_speed * dsin(angle);
    y -= max_speed * dcos(angle);
}
if(keyboard_check(ord("D"))){
    x += max_speed * dsin(angle);
    y += max_speed * dcos(angle);
}
 
Top