GML Set image angle with WASD

M

MrSanfrinsisco

Guest
I've came across something I've never done and I can't even begin to fathom how I would go about doing this. I want my character to rotate depending on which way I am moving but not with the mouse, I can do it with the mouse however there is no mouse required in this game. I want my character to rotate depending on which way I am moving using WASD. Right now my character only faces up, left, right, or downward but he does not face diagonally and that's what I'm trying to get him to do. Would I have to use a function like lengthdir_x and lengthdir_y? Or is that totally wrong.
 

Paskaler

Member
You won't need those to find the angle. Here's something that should work:
Code:
var delta_x = keyboard_check(ord('D')) - keyboard_check(ord('A'));
var delta_y = keyboard_check(ord('S')) - keyboard_check(ord('W'));

var angle = point_direction(0, 0, delta_x, delta_y);
If you wish to move the player without him going faster diagonally, then you'll have to use those functions:

Code:
x += lengthdir_x(angle, move_speed);
y += lengthdir_y(angle, move_speed);
Note that you won't have acceleration using this code or collisions and such
 
M

MrSanfrinsisco

Guest
You won't need those to find the angle. Here's something that should work:
Code:
var delta_x = keyboard_check(ord('D')) - keyboard_check(ord('A'));
var delta_y = keyboard_check(ord('S')) - keyboard_check(ord('W'));

var angle = point_direction(0, 0, delta_x, delta_y);
If you wish to move the player without him going faster diagonally, then you'll have to use those functions:

Code:
x += lengthdir_x(angle, move_speed);
y += lengthdir_y(angle, move_speed);
Note that you won't have acceleration using this code or collisions and such
Okay, so that kind of worked. It works in the sense he does look diagonally but it doesn't work in the sense that when he moves diagonally he moves sideways. It's probably something in the way the sprite is facing when I created him. Is there a way to fix that?
 

Bingdom

Googledom
You could set the angle like this:
Code:
angle = point_direction(A,S,D,W)
Are your floor()'ing the coordinates after moving the player? That normally messes up movement if the speed is less than sqrt(2).
Try flooring the camera/view coordinates instead of the player.

Make sure the sprite is facing to the right.
 
M

MrSanfrinsisco

Guest
You could set the angle like this:
Code:
angle = point_direction(A,S,D,W)
Are your floor()'ing the coordinates after moving the player? That normally messes up movement if the speed is less than sqrt(2).
Try flooring the camera/view coordinates instead of the player.

Make sure the sprite is facing to the right.
Yea, that's a lesson learned. I didn't know sprites had to be facing a specific way

and that's what I was trying to do but didn't know how to do it. Store which way they were moving in a variable
 
Top