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

Комментарии • 134

  • @bakpakgoblin
    @bakpakgoblin 10 месяцев назад +7

    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;
    }

    • @edujamerbrz
      @edujamerbrz 7 месяцев назад +3

      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;
      }

    • @edujamerbrz
      @edujamerbrz 7 месяцев назад +2

      Prob not gonna help you cuz y know, 3 months, but idc

  • @ksioncdesign7075
    @ksioncdesign7075 6 лет назад +77

    the best GM youtuber

    • @ron0gamingplayz102
      @ron0gamingplayz102 5 лет назад +10

      tied first place with Shaun Spalding

    • @ksioncdesign7075
      @ksioncdesign7075 5 лет назад +4

      @@ron0gamingplayz102 FriendlyCosmonaut > Shaun Spalding

    • @jokezspeedruns9679
      @jokezspeedruns9679 4 года назад +2

      @@ksioncdesign7075 FriendlyCosmonaut >= Shaun Spalding*

  • @cookiesdll
    @cookiesdll 5 лет назад +17

    I love how you explain how and why things do what they do. And it's not boring!

  • @wadalapichu4873
    @wadalapichu4873 8 дней назад

    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!

  • @royaljunior2125
    @royaljunior2125 6 лет назад +10

    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!

  • @Retrofire-47
    @Retrofire-47 6 лет назад +2

    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!!

  • @seeker1602
    @seeker1602 3 года назад +1

    Elegant and simple... Between you and Shaun Spaulding I'm making beautiful things... Thanks.

  • @originalgrim8750
    @originalgrim8750 6 лет назад +6

    This was something I was wanting to learn to do I'm happy you made this :)

  • @MrMcJazzhands
    @MrMcJazzhands 4 года назад

    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!

  • @erzloh
    @erzloh 6 лет назад

    Very comprehensive, thank you for the tutorial ! Keep the good work !

  • @NeZversSounds
    @NeZversSounds 6 лет назад +20

    For added isometric feel moveY can be multiplied by some kind ratio to reduce Y speed.

  • @stormmerm4776
    @stormmerm4776 6 лет назад

    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)

  • @nightwing2461
    @nightwing2461 6 лет назад +2

    Very well put together in a fairly easy descriptive manner, thank you!

  • @rindallbroxmitten
    @rindallbroxmitten 4 года назад

    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.

  • @nina8711
    @nina8711 6 месяцев назад +1

    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?)

  • @WimeSTone
    @WimeSTone 6 лет назад +2

    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

    • @jasperplayz2138
      @jasperplayz2138 5 лет назад

      How to make a collision system for this type of movement? I can't seem to figure it out.

  • @fihrilkamal7296
    @fihrilkamal7296 5 лет назад +1

    Amazing tutorial, very clear, I'm still using GM 1.4 but it's totally applicable.

  • @dubtron_
    @dubtron_ 3 года назад +3

    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"?

  • @WildDisease72
    @WildDisease72 4 года назад

    Amazing! I used x,y instead of 0,0 in point_direction and I was getting weird results but 0,0 is genius!

  • @raindancer5143
    @raindancer5143 Год назад

    still very useful in 2023, thanks

  • @x8squish8x16
    @x8squish8x16 6 лет назад +1

    She's back again

  • @gamemaker5644
    @gamemaker5644 3 года назад +1

    How about a video with 8 directional movement + 8 directional shooting?

  • @sanstheanimatronic5542
    @sanstheanimatronic5542 3 года назад

    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

  • @scalyvince4220
    @scalyvince4220 3 года назад

    you are the best person

  • @jarirepo1172
    @jarirepo1172 Год назад

    Thanks, very good video!

  • @gelatinousgames3810
    @gelatinousgames3810 5 лет назад +3

    how would i code in collision for this system?

  • @thinkinges2497
    @thinkinges2497 4 года назад +1

    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?

  • @thenekronomikon
    @thenekronomikon 2 года назад +2

    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

    • @curissss
      @curissss 2 года назад

      i follow the directions in the video and my sprite wont move :(
      do you know how to fix this?

  • @iKiwed
    @iKiwed 6 лет назад +2

    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!

    • @iKiwed
      @iKiwed 6 лет назад

      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..

  • @TheArchitectOfDreams
    @TheArchitectOfDreams 2 года назад

    Great Tutorial! I like it.

  • @vort-x4896
    @vort-x4896 3 года назад +1

    im working on a horror game and i would like to know if i can use the tutorial sprites (the grey one)

    • @benthefl3124
      @benthefl3124 3 года назад

      Links normally I'm her vid descriptions she has another link om how to credit her

  • @selenearts4933
    @selenearts4933 5 лет назад

    Very useful since a lot of different game genres use this.

  • @turbodrift255
    @turbodrift255 5 лет назад

    Thank you for this impressing tutorial! You helped me a lot!

  • @danielobrien5216
    @danielobrien5216 2 года назад

    Thank you so much For the tutorial!

  • @saxenart
    @saxenart 2 года назад +1

    How do you do 4 movements?

  • @Troylglover
    @Troylglover 4 года назад +1

    how do I make my player shoot in the direction their facing? (without a mouse)

  • @colettelevesque249
    @colettelevesque249 5 лет назад

    So well explained. Thank you!

  • @Psychotyzm
    @Psychotyzm Год назад +1

    oh my GOD, I was struggling with the diagonal movement so much...

  • @Scarabola
    @Scarabola 4 года назад +3

    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.

    • @theallseeinghat9484
      @theallseeinghat9484 4 года назад +2

      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

    • @DanielAlvarez-oq8ex
      @DanielAlvarez-oq8ex 3 года назад

      @@theallseeinghat9484 thanks and you are too

  • @maxredd4904
    @maxredd4904 5 лет назад

    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?

  • @ab-vq3yy
    @ab-vq3yy 3 года назад

    i did the same thing but my movement player cant move down

  • @pokirussgamedev530
    @pokirussgamedev530 3 года назад

    my brain is expanding and I don't know if i'm in danger or not

  • @ianainoaduarte
    @ianainoaduarte 5 лет назад +3

    how do you do colisions with this method of movement?

    • @AnswerDoesNotExist
      @AnswerDoesNotExist 2 года назад

      I have the same question and you
      haven't been answered for 2 years
      Sad.

  • @2wugs
    @2wugs 3 года назад

    thanks, I just got gamemaker after trying unity and it fits my needs better.

  • @yektaesmaeili7589
    @yektaesmaeili7589 4 года назад

    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?

  • @MkaApps
    @MkaApps 4 года назад

    im getting fatal error at the first run it is because i have gamemaker profesional edition?

    • @benthefl3124
      @benthefl3124 3 года назад

      Double check your code I guess, welcome to coding my friend, 1 missing comma and Ur done for

  • @deadlyquestion
    @deadlyquestion 4 года назад +1

    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)

  • @theallseeinghat9484
    @theallseeinghat9484 4 года назад

    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.

  • @danimations80
    @danimations80 4 года назад

    Thanks Cosmo :D

  • @rodut2432
    @rodut2432 3 года назад

    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"

  • @theinfamouspat5977
    @theinfamouspat5977 6 лет назад

    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 :)

  • @huangxin9500
    @huangxin9500 6 лет назад

    Just wondering why the variable like "hInput" and "moveX" does not defined in my GMS2, I have the same version with yours.

  • @steveschreiner7444
    @steveschreiner7444 4 года назад

    Thank you, this detailed explanation helps a lot not to just copy the code

  • @TheMasterKnightKingo
    @TheMasterKnightKingo 3 года назад

    Muchas gracias

  • @x8squish8x16
    @x8squish8x16 6 лет назад

    Are going to finish that other vid

  • @jasperplayz2138
    @jasperplayz2138 5 лет назад +1

    How to make a collision system for this type of movement? I can't seem to figure it out.

    • @Tecamech
      @Tecamech 5 лет назад +3

      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.

    • @jasperplayz2138
      @jasperplayz2138 5 лет назад

      @@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!

    • @yoririuy
      @yoririuy 5 лет назад

      @@jasperplayz2138 where you found this tutorial? can you send me the link?.

    • @jasperplayz2138
      @jasperplayz2138 5 лет назад

      @@yoririuy www.yoyogames.com/blog/432/buttery-smooth-tech-tips-movement

    • @worldwide0218
      @worldwide0218 5 лет назад

      @@Tecamech thank you
      you helped me so much :)

  • @xeonviento9778
    @xeonviento9778 6 лет назад +1

    Could you make a video on how to tackle that screen tearing on the sprite while its moving (first 10 seconds)?

    • @wilhelmgstudios8594
      @wilhelmgstudios8594 3 года назад +1

      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.

  • @mitsuck7881
    @mitsuck7881 5 лет назад +1

    how do i make him look at the cursor (just like hotline miami)

    • @nitneat
      @nitneat 5 лет назад

      player step event:dir = point_direction(x,y,mouse.x,mouse.y);

  • @reuzlwickluwican2351
    @reuzlwickluwican2351 3 года назад

    Is it possible to code my character move in 20 or more directions in game maker? Pls reply

  • @sheshin
    @sheshin 6 лет назад

    Can this be used for twin stick so the right stick changes the direction and if oposite the sprite plays backwards?

  • @diosensopp
    @diosensopp 4 года назад

    just perfect !!

  • @Troylglover
    @Troylglover 4 года назад

    this works really great. Could you please teach us how to make our enemy randomly move in 8 directions?

  • @Narque
    @Narque 4 года назад

    how can I change this so it can be used with mouse to click movement?

  • @doogan807
    @doogan807 6 лет назад

    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?

    • @Ashley-xs4ic
      @Ashley-xs4ic 5 лет назад

      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?

  • @ohlyptash
    @ohlyptash 3 года назад

    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;
    }

    • @ohlyptash
      @ohlyptash 3 года назад

      Ok, i did a mistake, in Create I put speed = 2 and then spd = speed. I know its stupid but leave it here.

  • @mithrandrilltharkun1970
    @mithrandrilltharkun1970 5 лет назад

    Great job. Maybe link to next video is all.

  • @MrSchmatze
    @MrSchmatze 6 лет назад

    So helpful! Thank you so much !:)

  • @fierorecensione5828
    @fierorecensione5828 Год назад

    top video

  • @Beologe
    @Beologe 3 месяца назад

    awesome :)

  • @MS-pz9wd
    @MS-pz9wd 2 месяца назад

    is this better than move_and_collide?

  • @valorant_kingdoms
    @valorant_kingdoms 5 лет назад

    love it, thanksss

  • @Mystical-TEDDY_
    @Mystical-TEDDY_ 3 года назад

    how do u make it wasd instead of arrow keys

    • @Leifuren
      @Leifuren 3 года назад

      keyboard_check(ord("W"));

  • @tyrondeonarine1348
    @tyrondeonarine1348 5 лет назад

    Thank you.

  • @x8squish8x16
    @x8squish8x16 6 лет назад +1

    Let's go

  • @frozentiger4896
    @frozentiger4896 7 месяцев назад

    My player is super shaky when I use this code

  • @okamiwon129
    @okamiwon129 5 лет назад

    Please help when i click right or left it moves only diagonally, and the down and up key doesn't work, PLEASE HELP

    • @clad2544
      @clad2544 5 лет назад

      Where you have copy and pasted "moveX = lengthdir_x(spd, dir); make sure you change "moveX" to "moveY" and "lengthdir_x" to "lengthdir_y"

    • @okamiwon129
      @okamiwon129 5 лет назад

      @@clad2544 thx

    • @benthefl3124
      @benthefl3124 3 года назад

      @@clad2544 lmao I feel dumb now

  • @_david_548k_6
    @_david_548k_6 Год назад

    I love this fucking channel, thank for all and sorry for nothing!

  • @wenze1989
    @wenze1989 4 года назад

    thank you very much:)

  • @_isaac_muniz_
    @_isaac_muniz_ 5 лет назад

    Perfect

  • @imperialwyvern
    @imperialwyvern 4 года назад

    where do I go to program movement?

  • @cellebscalls
    @cellebscalls 2 года назад

    Nice

  • @paulscalise2329
    @paulscalise2329 6 лет назад +5

    What about collision?

    • @chrisbee1984
      @chrisbee1984 5 лет назад +1

      I would like to know this as well

  • @DonGeritch
    @DonGeritch 6 лет назад +1

    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!

    • @stormmerm4776
      @stormmerm4776 6 лет назад

      are you using a gamepad or following a cursor for that 360 degrees of freedom?

    • @DonGeritch
      @DonGeritch 6 лет назад

      @@stormmerm4776 atm I'm using mouse cursor, but I might want to add gamepad as an option

    • @stormmerm4776
      @stormmerm4776 6 лет назад

      @@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

  • @powersourzecartoons8008
    @powersourzecartoons8008 5 лет назад

    How do your write the code?

  • @VojBer
    @VojBer 6 лет назад

    Christmas present :)

  • @Liam-d7o
    @Liam-d7o Год назад

    Is anybody else's character stretching weird while going diagonal?

  • @boinky92
    @boinky92 6 лет назад

    Your my favorite

  • @nova2566
    @nova2566 6 лет назад

    Yaaaay! :D

  • @leondecastro1807
    @leondecastro1807 6 лет назад +2

    Ow yee

    • @robos
      @robos 6 лет назад +1

      Do you have ligma?

    • @aliakeel
      @aliakeel 5 лет назад

      @@robos no

  • @had1t1x85
    @had1t1x85 6 лет назад +1

    Those thicc arms tho

  • @wishifil
    @wishifil Год назад

    anybody else’s get blurry when they walk?

  • @TheCivildecay
    @TheCivildecay 5 лет назад

    Gamemaker would be so much easier to use if they included vector2's

  • @jacobbergholtz4032
    @jacobbergholtz4032 4 года назад

    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

  • @efkansenol
    @efkansenol 3 года назад

    the best GM youtuber

  • @CraftingDiamond
    @CraftingDiamond 5 лет назад +6

    How can i do collision with this movement method?

    • @snoopy7172
      @snoopy7172 4 года назад +1

      Probably a little late but just the same as normal with collisions on thinga

  • @AdamBarkerThe
    @AdamBarkerThe 6 месяцев назад

    awesome tutorial thanks a lot for this.