Fall, Jump, Gravity in Godot. How to move_and_slide tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ก.ย. 2024
  • I didn't find any tutorial which is explaining writing gravity for kinematic character 2d, so... I recorded it:D
    --------------- Ways to Support ---------------
    💝 Donate My Game: rafafiedo.itch...
    ☕️ Coffee: ko-fi.com/rafa...
    --------------- Useful links ---------------
    ✎ Source code: github.com/she...
    ✏ My Godot projects: github.com/rfi...
    --------------- Contact and social media ---------------
    💬 Discord: / discord
    🐝 Twitter: / rafafiedo
    ♫ Music channel: / rafafiedomusic
    ✉ Mail: fiedorowicz.rafal@gmail.com
    Don't forget to leave a comment to let me know if you enjoyed this video, it's important for me! :)
    Hope you'll like it!
    #godot #tutorial

ความคิดเห็น • 72

  • @RafaFiedo
    @RafaFiedo  4 ปีที่แล้ว +4

    You can find more my Godot project here: github.com/sheetcode/GodotProjects :D

  • @codyromano7868
    @codyromano7868 2 หลายเดือนก่อน +1

    This tutorial is extremely helpful! Thanks for the explanation of timers, gravity, and jumping. 🙏 (Constructive feedback: please consider making future tutorials without the acoustic music because the music makes it a bit harder to follow what you're saying.)

    • @RafaFiedo
      @RafaFiedo  2 หลายเดือนก่อน

      Thanks for feedback!
      Yea, in future tutorials I used 'background music' only in fun part and for pure teaching I left silence.
      That tutorial has more than 4 years :D
      What do you think? Are people interested in remaking these old tutorials?
      I wonder how you found this one

  • @scarm_rune
    @scarm_rune 3 ปีที่แล้ว +7

    this man saved me from destroying my keyboard
    thanks

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      Haha, glad that you don't need to buy a new one! 😉

  • @AnthonyHarivel
    @AnthonyHarivel 4 ปีที่แล้ว +6

    Thanks !!!! Now I understand the numbers I see on every tuto for moving the player. Just have to do the math ! :)

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว +1

      Glad it helped! Sadly a lot of youtubers don't understand these numbers and are increasing "speed" with delta value, but in the other hand I also did that for the begging :P

    • @AnthonyHarivel
      @AnthonyHarivel 4 ปีที่แล้ว +1

      @@RafaFiedo this was really interesting indeed ! Now I want to experiment a bit more with gravity and write this down to keep as a reference in order to find out the best numbers for the coolest best game feeling !! :)

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว

      great! Can't wait:)

  • @izyaboicallume
    @izyaboicallume 3 ปีที่แล้ว +1

    "Player! I have bad news... Unfortunately, There Is No Gravity"

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      haha, perfect sentence :D

  • @RandomUser_543
    @RandomUser_543 ปีที่แล้ว +1

    Thank you

  • @micmacha
    @micmacha ปีที่แล้ว

    I'm pretty sure that you can simply multiply the acceleration by delta. What you're doing here appears to work, but it depends way too much on consistent updates per second, which we don't always get.

    • @RafaFiedo
      @RafaFiedo  ปีที่แล้ว

      You can do this ;) It depends of the result you want to achive
      If you want do more official way it is not recommend to multiply by delta (as the engine doing it already -> official documentation)
      KinematicBody2d -> move_and_slide function
      "This method should be used in Node._physics_process (or in a method called by Node._physics_process), as it uses the physics step's delta value automatically in calculations. Otherwise, the simulation will run at an incorrect speed."
      Please correct me, if I missunderstood!

  • @blamechickenman7434
    @blamechickenman7434 2 ปีที่แล้ว +4

    I know I'm very late, but when I ran the code before you added left and right, I was able to infitately jump up. I fixed this by:
    if (Input.is_action_just_pressed("ui_up")):
    var collision_info = move_and_collide(velocity * delta)
    if collision_info:
    velocity.y -= 25
    Does this look good or is there a change I can make>

  • @lennertplayz
    @lennertplayz 2 ปีที่แล้ว +1

    One problem: when the player touches the ground, velocity isn´t reset. The player keeps accumulating velocity and virtually sticks to the ground.
    SOLVED!! I´ll leave it here for others. Make sure to not do the "move_and_slide(velocity)", but "velocity = move_and_slide(velocity)" as CORRECTLY shown here.

    • @RafaFiedo
      @RafaFiedo  2 ปีที่แล้ว

      haha :)
      Thanks for sharing it!

  • @MrAstarcius-Chan
    @MrAstarcius-Chan ปีที่แล้ว

    your a legend

  • @domeen0gt895
    @domeen0gt895 4 ปีที่แล้ว +6

    Hi There! I was wondering if you are up for an interview?

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว +2

      Hi, yea sure. What kind of interview?

    • @domeen0gt895
      @domeen0gt895 4 ปีที่แล้ว +2

      @@RafaFiedo We will be talking about either one game of your choice, of, if you aren't focusing on one project your general game dev journey, maybe we should move this on discord

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว +2

      sounds nice, SheetCode#5841

  • @hexag0nyt283
    @hexag0nyt283 3 ปีที่แล้ว +1

    THANK YOU SO MUCH!

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      You're welcome!

    • @hexag0nyt283
      @hexag0nyt283 3 ปีที่แล้ว

      @@RafaFiedo hey, i wanted to make it so if the player isnt moving / has stopped moving, a timer will start and when it timeouts, it will play an animation. Do you know how?

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      @@hexag0nyt283
      In the process function you can write a condition
      if player_stoped and timer don't run
      then start the timer
      and use timer signal to do
      AnimationPlayer.play("animation_name")
      Have you tried something like this?

    • @hexag0nyt283
      @hexag0nyt283 3 ปีที่แล้ว

      @@RafaFiedo i tried starting the timer after input.is_action_just_released("left/right") but i dont think it started the time (im a beginner btw)

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      @@hexag0nyt283 I think it will be easier if you write me a message with code in discord ;)

  • @darealspawn
    @darealspawn ปีที่แล้ว +1

    Could you do one with the characterbody2d in later versions of godot ?

    • @RafaFiedo
      @RafaFiedo  11 หลายเดือนก่อน

      thanks for asking ;)
      not sure about godot 4 tutorials, will see ;)

  • @Code_Machine
    @Code_Machine 4 ปีที่แล้ว +2

    I like your videos, but please do not cut out the parts where you modify the code as you did when you fixed the gravity

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว +1

      Thanks for your feedback! Are you saying when Gravity changed from 10 to 0.1? Sorry for that, but I missed that in the editing. Still learning how to do proper tutorials ;)

    • @Code_Machine
      @Code_Machine 4 ปีที่แล้ว

      @@RafaFiedo Yes that part. I also have a question regarding that. In the video you basically explained that acceleration += (gravity * delta), but you just changed gravity from 10 to delta (or 0.1 to be precise). How does that work?

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว +1

      @@Code_Machine
      at 6:02 I'm trying to explain how the "acceleration is working" and why gravity "10" is too much.
      next at the 6:30, I used gravity 0.01 (which is not delta, because delta is equal to 0.016666.. btw. it is a funny coincidence), because it used an example where I have easy numbers to teach this method :) You can use smaller/ lower number there. The speed graph for G = 0.01 you can see here 7:15
      How it is work? At 6:30 you have an example of what speed you will have frame per frame. I'm using also "delta" in the calculations because function "move_and_slide" has delta calculated inside a function. More about this function you can also read on the documentation:
      "This method should be used in Node._physics_process (or in a method called by Node._physics_process), as it uses the physics step's delta value automatically in calculations. Otherwise, the simulation will run at an incorrect speed." from docs.godotengine.org/en/stable/classes/class_kinematicbody.html#class-kinematicbody-method-move-and-slide
      hope I helped ;)

    • @Code_Machine
      @Code_Machine 4 ปีที่แล้ว

      @@RafaFiedo That is interesting. I noticed you have a discord. Do you mind if I contact you on that?

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว +1

      No problem, I’m available on discord. Mostly time invisible ;)

  • @paulus6809
    @paulus6809 2 ปีที่แล้ว

    For me, I always recieve the error ""move_and_slide" isn´t declared in the current class". I´m not really sure what I need to do since I do not see these errors pop up in any tutorials...

    • @RafaFiedo
      @RafaFiedo  2 ปีที่แล้ว

      hi!
      Could you tell me what do you have on first line of your script?
      'extends... '

  • @AlternateNoeh
    @AlternateNoeh 2 ปีที่แล้ว +1

    controls not working? i dont understand the code is perfect i have bindings but nothing moves it

    • @RafaFiedo
      @RafaFiedo  2 ปีที่แล้ว

      Hi ZapKaner,
      Sorry to hear that the code isn't working. It can be a lot of things :)
      Try to use debug to detect if the code is connect in the right way. If you don't know what it is you can use a lot of "print("test1") to check which code is working

    • @AlternateNoeh
      @AlternateNoeh 2 ปีที่แล้ว

      @@RafaFiedo it's not the code I downloaded the game to see if a did it right, I think Its just the engine I'm not really sure

  • @mythqil3696
    @mythqil3696 3 ปีที่แล้ว +1

    Great video but im trying to just add gravity to one object thats not a player any help?

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      It's depend of what do you exactly want do to with this object :P Adding basic gravity is easy because, but I suspect that you want also "move" objects etc. That means a lot to do ;)
      Did you hear about "rigid body"? Maybe that will be nice for your goal

  • @arcticblitz9630
    @arcticblitz9630 3 ปีที่แล้ว +1

    How do I make it so people can only jump once until they hit the ground again?

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      Hi Arctic Blitz!
      Do you still have an issue with it?
      As short answer I can suggest to use the "is_on_floor" function from docs.godotengine.org/en/stable/classes/class_kinematicbody2d.html#class-kinematicbody2d-method-is-on-floor

    • @arcticblitz9630
      @arcticblitz9630 3 ปีที่แล้ว

      @@RafaFiedo thanks!

    • @arcticblitz9630
      @arcticblitz9630 3 ปีที่แล้ว

      @@RafaFiedo ok so my problem now is after using the is_on_floor function, my player doesn't want to move. It can still move left and right, just not up

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว

      @@arcticblitz9630 Did you play my game Hammer King? It's simple platform game. I also added source code. In this part github.com/rfiedorowicz/Hammer-King/blob/master/scripts/Human.gd#L27 You can see how I'm using "is_on_floor" function ;)

    • @arcticblitz9630
      @arcticblitz9630 3 ปีที่แล้ว +1

      @@RafaFiedo oki, thanks! I'll go check the game out

  • @reanibutani9555
    @reanibutani9555 3 ปีที่แล้ว +1

    How to move_and_slide_with_snap

    • @RafaFiedo
      @RafaFiedo  3 ปีที่แล้ว +1

      Is it proposition for next tutorial?:)

  • @KelvinCipta
    @KelvinCipta 2 ปีที่แล้ว

    Just use rigidbody2d

  • @kito9690
    @kito9690 4 ปีที่แล้ว

    when I go to test an error happens, it is "Attempt to call function 'Start' in base 'null instance' on a null instance"

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว

      Probably you are using Start method to node class with isn’t created, try to use built it debugger to check the variable, you can also copy paste your script and maybe I will find something more ;)

    • @kito9690
      @kito9690 4 ปีที่แล้ว

      @@RafaFiedo ok, I read the debugger and said that I couldn't find the node timer

    • @kito9690
      @kito9690 4 ปีที่แล้ว

      I managed to solve the error problem, but it still doesn't work

    • @RafaFiedo
      @RafaFiedo  4 ปีที่แล้ว

      @@kito9690 Have you tried my project from git? github.com/sheetcode/tutorial_movement is it working?

    • @kito9690
      @kito9690 4 ปีที่แล้ว

      I saw your project now, it’s not working, they probably changed something that made this system not work

  • @Vaska-se8br
    @Vaska-se8br ปีที่แล้ว

    Мне кажется у тебя русский акцент )

    • @RafaFiedo
      @RafaFiedo  ปีที่แล้ว

      miss, but no so much. I'm polish ;)

    • @Vaska-se8br
      @Vaska-se8br ปีที่แล้ว +1

      @@RafaFiedo Ohh... Good )

  • @mingji753
    @mingji753 2 ปีที่แล้ว

    hmm, bro you are so slow!!

    • @RafaFiedo
      @RafaFiedo  2 ปีที่แล้ว

      you can always speed up the video ;)