8 Directional Movement | GMS2 [1]
HTML-код
- Опубликовано: 9 фев 2025
- In this tutorial, we learn how to program 8-directional movement. Whereas simplistic movement logic can mean the player moves faster on diagonals, we maintain the same speed in every direction!
Sprites download:
1drv.ms/f/s!Am...
Sprites credit:
Adapted from TheNess "RPG sprite - 8 direction human male 16x16": opengameart.or...
Here I am in other places:
FriendlyCosmonautDev channel: / @friendlycosmonautdev8040
Twitter: / friendly_cosmo
Patreon: / friendlycosmonaut
In case anyone needs it, My game was crashing because it didn't recognize spd. I specified it as a variable and the code below worked for me. No idea what I'm doing, but hope it helps someone.
(The keyboard_check(ord("D") etc. is me changing the controls from the arrow keys to ASWD)
var hInput = keyboard_check(ord("D")) - keyboard_check(ord("A"));
var vInput = keyboard_check(ord("S")) - keyboard_check(ord("W"));
var spd = 4
if(hInput != 0 or vInput != 0){
dir = point_direction(0,0,hInput,vInput);
moveX = lengthdir_x(spd, dir);
moveY = lengthdir_y(spd, dir);
x += moveX;
y += moveY;
}
I know how to fix, (i had the same problem) if it helps:
//Create event
spd = 4
//Step event
hInput = keyboard_check(ord("D")) - keyboard_check(ord("A"));
vInput = keyboard_check(ord("S")) - keyboard_check(ord("W"));
if(hInput != 0 or vInput != 0){
dir = point_direction(0,0,hInput,vInput);
moveX = lengthdir_x(spd, dir);
moveY = lengthdir_y(spd, dir);
x += moveX;
y += moveY;
}
Prob not gonna help you cuz y know, 3 months, but idc
the best GM youtuber
tied first place with Shaun Spalding
@@ron0gamingplayz102 FriendlyCosmonaut > Shaun Spalding
@@ksioncdesign7075 FriendlyCosmonaut >= Shaun Spalding*
I love how you explain how and why things do what they do. And it's not boring!
this is an awesome tutorial, i was about to stick with multiplying the speed with .701 (or something like that) when the player makes diagonal movement, but i prefer this cleaner method, your explanation was amazing and it cleared a lot of things to me, thank you very much!
Thank you for making this! I was totally wanting to make an rpg with this kinda movement, plus I learned more than I thought I would. So thanks again!
I'm really glad I got into gamemaker when I did. Your latest videos just happened to release just as I was starting to learn animations.
Thank you!!
Elegant and simple... Between you and Shaun Spaulding I'm making beautiful things... Thanks.
This was something I was wanting to learn to do I'm happy you made this :)
I just started working on a side-scrolling shooter in the vein of Gradius the other day and this answered every question I had. Thank you!
Very comprehensive, thank you for the tutorial ! Keep the good work !
For added isometric feel moveY can be multiplied by some kind ratio to reduce Y speed.
Instablaster
Really cool starter content :D - the pythagorean property sometimes slips the mind for newcomers and they don't get just why moving on the diagonals is faster
For those in the comments that want to this, but for every direction on a gamepad you can do:
hInput = gamepad_axis_value(0, gp_axislh);
vInput = gamepad_axis_value(0, gp_axislv);
(0 as in the first gamepad; use 1 for the second, 2 for the third so on; for the right axis handle, use gp_axisrh and gp_axisrl instead)
Very well put together in a fairly easy descriptive manner, thank you!
This is really cool to know!
There were a couple of games I played that didn't take this into account and moving diagonally was faster, so I moved diagonally in those games. I wish I could remember some titles, though.
its not working? Im still at the start kind of area and whenever i do her code it doesnt recognise hInput or vInput and im not sure how to turn it into a variable correctly as im only a beginner so far. Ive tried to just put var infront of the statement but nothing happens, so then i move it to the create event like gms2 was recommending me but still nothing? Anyways im lost and at this point would love just an explanation on what may be happening and give me the code (im also using WASD instead and dont worry im doing the correct keyboard_check(ord("D")) - keyboard_check(ord("A")); whole shenanigans and only doing 4 directions so 0, 90, 180, and 270 incase this may have anything to do with the problem?)
A little info. A lot of manufacturers don't put "anti-ghosting" on arrow keys. Note, a correct term is "keyboard rollover", marketing is a scary thing. It means, when for example, you press and hold Left, Right, then Down - keyboard might register only Left and Right, ignoring Down completely. Plus, a lot of keyboards don't register Left+Up+Space key combination, while registering Right+Up+Space. It is pure hardware problem, it has nothing to do with GM. Switch to WASD, since that block is pretty much always covered with anti-ghosting. This is the main reason, why WASD is used so much. Even my fancy Razer keyboard can only register 2 arrow keys. If you are curious about yours - google "keyboard rollover test".
Have a nice day :D
How to make a collision system for this type of movement? I can't seem to figure it out.
Amazing tutorial, very clear, I'm still using GM 1.4 but it's totally applicable.
I'm trying to use the WASD keys for movement rather than the arrow keys. What should I type for each spot that has "vk_up" "vk_down" "vk_left" "vk_right"?
did you ever figure it out. I want to know too
Use ord
Amazing! I used x,y instead of 0,0 in point_direction and I was getting weird results but 0,0 is genius!
still very useful in 2023, thanks
She's back again
How about a video with 8 directional movement + 8 directional shooting?
why does it move on its own
as soon as i try to change the speed when i open the game the sprite instantly starts moving to the right and wont stop and i did everything you this also happened to me when i tried another tutorial for some reason the object moves on its own only when i add an event create and try to change the speed
you are the best person
Thanks, very good video!
how would i code in collision for this system?
Hmm, very good. just the room speed isnt getting the sprite movement, so looks the worst i ever see when moving diagonally, some help to let the room follow that movement and stop looking like is jumping each diagonal pixel?
To anyone looking to move with WSAD instead of the arrow keys:
hInput = keyboard_check(ord(("D"))) - keyboard_check(ord(("A")));
vInput = keyboard_check(ord(("S"))) - keyboard_check(ord(("W")));
hope this helps
i follow the directions in the video and my sprite wont move :(
do you know how to fix this?
Thank you, it’s actually something I wanted to know more. In my project of last year I did it thanks to easier codes and a bit of DnD, but I want to come back to my project and I think I need to do that with full codes even if I’m not good at it. I’ll wait for the next lesson and see how to fix the animation.
Peace!
There is another thing I really want an answer, and it’s basically how to jump in platforms like in Mario & Luigi. It’s one of the things I’d like to introduce, but I guess it takes a lot of time..
Great Tutorial! I like it.
im working on a horror game and i would like to know if i can use the tutorial sprites (the grey one)
Links normally I'm her vid descriptions she has another link om how to credit her
Very useful since a lot of different game genres use this.
Thank you for this impressing tutorial! You helped me a lot!
Thank you so much For the tutorial!
How do you do 4 movements?
how do I make my player shoot in the direction their facing? (without a mouse)
So well explained. Thank you!
oh my GOD, I was struggling with the diagonal movement so much...
So can you teach us how to implement collisions with this 8-directional system? Your collisions video doesn't seem to be compatible with this vid.
www.reddit.com/r/gamemaker/comments/c82nra/i_need_help_setting_up_collisions_with/
I had this same problem, and this reddit thread was a god send
@@theallseeinghat9484 thanks and you are too
so I try to do things before they're explained and see how close I got to the video, normally I switch over to what she did instead because it's better, but this time I'm wondering if there's a downside to the way I did it.
so instead of adding a spd variable and finding your direction using vectors and all that jazz I made a variable diag = 1; and wrote an if loop that checked if hInput and vInput were both not equal to zero, if that was the case I divided diag by the square root of 2, then just set x+= vInput * diag y += hInput * diag. this makes the character move at the proper speed and changing the value of diag functions the same as setting a spd variable. As far as I can tell this is functionally identical, is there a disadvantage to doing it that way?
i did the same thing but my movement player cant move down
my brain is expanding and I don't know if i'm in danger or not
how do you do colisions with this method of movement?
I have the same question and you
haven't been answered for 2 years
Sad.
thanks, I just got gamemaker after trying unity and it fits my needs better.
I did all this but when my character is staying still the picture is the one that it was doing before...for example if it was walking to right when it stop the pictures is for when it was walking...what should i do?
im getting fatal error at the first run it is because i have gamemaker profesional edition?
Double check your code I guess, welcome to coding my friend, 1 missing comma and Ur done for
Hello FriendlyCosmonaut! I wanted to make you aware that this first video in your 8 Directional Movement series does not appear in the associated playlist in your channel! (2 and 3 are there, but not 1)
I plan on making my character rotate with the mouse cursor, so instead of accessing the object's local axis's, how could I make them move on the global x and y axis? Cuz if the object rotates, then the controls are gonna rotate with it if that makes sense.
Thanks Cosmo :D
I have a problem. When I write :
moveX = lengthdir_x(move_spd,dir);
moveY = lengthdir_y(move_spd,dir);
just like you did, it says "variable moveX (and moveY) only referenced once"
How would i go about changing this from the arrow keys to WASD controls, do i need to do a keyboard_check_pressed('D') then have an IF command for the release? im fairly new sorry... great vid btw :)
Just wondering why the variable like "hInput" and "moveX" does not defined in my GMS2, I have the same version with yours.
Thank you, this detailed explanation helps a lot not to just copy the code
Muchas gracias
Are going to finish that other vid
How to make a collision system for this type of movement? I can't seem to figure it out.
It's important where the positioning of this chunk of code is going to be.
If you refer to cosmos collision video, you'll want to place this code after the reset code variables and before your intended movement.
Also go ahead and remove the "x += moveX;" and the "y += moveY;"
This will still give you eight directional movement while loading collision after.
Hopefully this fixes your issue.
@@Tecamech So much I figured. I've been using a tutorial from a blog-post on the gamemaker site, it was really helpful. Thank you all the same!
@@jasperplayz2138 where you found this tutorial? can you send me the link?.
@@yoririuy www.yoyogames.com/blog/432/buttery-smooth-tech-tips-movement
@@Tecamech thank you
you helped me so much :)
Could you make a video on how to tackle that screen tearing on the sprite while its moving (first 10 seconds)?
i know this is an old comment, but you just go into the game settings, then click on mac osx and windows, and theres a tickbox for "pixel tearing" you check that and it'll fix it.
how do i make him look at the cursor (just like hotline miami)
player step event:dir = point_direction(x,y,mouse.x,mouse.y);
Is it possible to code my character move in 20 or more directions in game maker? Pls reply
Pls reply
Can this be used for twin stick so the right stick changes the direction and if oposite the sprite plays backwards?
just perfect !!
this works really great. Could you please teach us how to make our enemy randomly move in 8 directions?
how can I change this so it can be used with mouse to click movement?
So, I used the code exactly as follows, but for some reason, when my character moves diagonally, they sort of shake. I’ve checked the resolution of my game to make sure it’s not trying to move it half pixels and I don’t think there’s any issue there. Also, once an input is received, the character never stops moving, but I think I can sort that out. Anyone know what could be causing the shake?
it might just seem like it's shaking because of the animation of the sprite keeps looping, it just looks kind of weird when it's going diagonally?
I did all like in video, but my object is always moving to the right. Did I something wrong?
Here's my code
hInput = keyboard_check(vk_right) - keyboard_check(vk_left);
vInput = keyboard_check(vk_down) - keyboard_check(vk_up);
if(hInput != 0 or vInput != 0) {
dir = point_direction(0,0,hInput,vInput);
moveX = lengthdir_x(spd, dir);
moveY = lengthdir_y(spd, dir);
x += moveX;
y += moveY;
}
Ok, i did a mistake, in Create I put speed = 2 and then spd = speed. I know its stupid but leave it here.
Great job. Maybe link to next video is all.
So helpful! Thank you so much !:)
top video
awesome :)
is this better than move_and_collide?
love it, thanksss
how do u make it wasd instead of arrow keys
keyboard_check(ord("W"));
Thank you.
Let's go
My player is super shaky when I use this code
Please help when i click right or left it moves only diagonally, and the down and up key doesn't work, PLEASE HELP
Where you have copy and pasted "moveX = lengthdir_x(spd, dir); make sure you change "moveX" to "moveY" and "lengthdir_x" to "lengthdir_y"
@@clad2544 thx
@@clad2544 lmao I feel dumb now
I love this fucking channel, thank for all and sorry for nothing!
thank you very much:)
Perfect
where do I go to program movement?
Nice
What about collision?
I would like to know this as well
nice video!
it would be great if you could also show how to make correct isometric movement with 360 degrees of freedom - how to get correct speed for every direction?
so insted of a circle, where its radius is responsible for the value of speed, we'll have a ellipse (a circle squished to a half of its height) - that means that speed would be different for every direction (1 horizontaly, 0.5 verticaly, and in every other direction something between)
my method was using a formula for finding this ellipse radius, but it didn't work well, probably because I was doing all the calculations in the single step event, but I'm not sure.. maybe it wasn't a right formula altogether..
anyway, I'm still looking for the solution, and if this is a problem you would be interested in making a video about it would be great!
cheers!
are you using a gamepad or following a cursor for that 360 degrees of freedom?
@@stormmerm4776 atm I'm using mouse cursor, but I might want to add gamepad as an option
@@DonGeritch
if point_distance(x,y,mouse_x,mouse_y)>1 //so that it won't shake constantly when touching the cursor point
dir=point_direction(x,y,mouse_x,mouse_y) // we're talking about the x and y of the object that follows
then do the movement like in the vid
How do your write the code?
Christmas present :)
Is anybody else's character stretching weird while going diagonal?
Your my favorite
Yaaaay! :D
Ow yee
Do you have ligma?
@@robos no
Those thicc arms tho
anybody else’s get blurry when they walk?
Gamemaker would be so much easier to use if they included vector2's
I made a video on this a few years ago that does the same thing but a bit differently. Your video is an interesting (and arguably more “concrete”) approach using trig even tho you don’t really see it when using point_dir.
If you can excuse my 16/17yo anxiety-riddled self, here’s the old video ruclips.net/video/9Fjke0xXBSw/видео.html
the best GM youtuber
How can i do collision with this movement method?
Probably a little late but just the same as normal with collisions on thinga
awesome tutorial thanks a lot for this.