Build a 2D GODOT Platformer in 20 Minutes!

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ส.ค. 2024
  • 👍👍👍 and subscribe for more GODOT tutorials: / @and1hof
    Check out my best selling AppSec book: amzn.to/3pGO4Vz
    Check out my behind-the-scenes newsletter: www.andrewhoff...
    In this video, learn how to build a complete 2D GODOT platformer game in just under 20 minutes. We cover the core fundamentals of movement, including gravity - tilemaps, tilesets, player input, collisions and GODOT fundamentals like the purpose of the physics process and how/where to customize it.

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

  • @reric88
    @reric88 11 หลายเดือนก่อน +6

    If you're using Godot 4, the video tutorial won't work as-is. Here's what I came up with (thanks to @JustinChaput). I added acceleration/deceleration to the movement, and changed the jump height based on your running speed. I also filled it with comments to explain what each part is doing
    extends CharacterBody2D
    var spd : int = 0
    var jump_spd : int = 200
    var gravity : int = ProjectSettings.get_setting("physics/2d/default_gravity")
    var height : int = 200
    func _physics_process(delta):
    # set speed of player
    # I will reference spd and SPEED. spd is referencing the variable (positive or negative)
    # whereas SPEED will always represent the actual movement speed (always positive)
    velocity.x = spd
    # move right
    if Input.is_physical_key_pressed(KEY_D):
    # if key is pressed, increase right SPEED by increments of 5 per tic until 200,
    # then stay at 200
    spd += 5
    if spd >= 200:
    spd = 200
    # move left
    if Input.is_physical_key_pressed(KEY_A):
    # if key is pressed, increase left SPEED by increments of 5 per tic until 200,
    # then stay at 200 (but is a negative value so it knows to move left)
    spd -= 5
    if spd 0:
    # if key is not pressed and spd value is greater than 0 (to prevent activation
    # while moving left), decrease SPEED by 6 per tic
    spd -= 6
    if spd < 4 && spd > -4 && not Input.is_physical_key_pressed(KEY_A) && not Input.is_physical_key_pressed(KEY_D):
    # if speed is between 4 and -4 and no direction is pressed, spd variable = 0
    # (to prevent unwanted movement while no key is pressed.)
    # I chose 4 and -4 so the spd doesn't bounce between 6 and -6 from above
    spd = 0
    # gravity
    if not is_on_floor():
    # if not currently jumping
    velocity.y += gravity * delta
    # jump
    if Input.is_action_just_pressed("jump") && is_on_floor():
    # if jump is pressed and player is currently on the floor
    if spd > 100:
    # if the spd is greater than 100, multiply spd by 1.7
    # (to increase jump height by your speed), and subtract that from velocity.y
    # this is for right movement, because you need a negative number to go up
    # (negative y is up on the screen)
    velocity.y -= spd * 1.7
    if spd < -100:
    # if the spd is less than -100, multiply spd by 1.7
    # and subtract it from velocity.y (to remain negative)
    velocity.y += spd * 1.7
    if spd > -100 && spd < 100:
    # default jump value
    velocity.y -= jump_spd
    # run the movement function
    move_and_slide()

  • @JustinChaput
    @JustinChaput ปีที่แล้ว +15

    On Godot 4 the player is CharacterBody2d
    Here is the code for player Godot 4 :
    extends CharacterBody2D
    var speed : int = 300
    var jump_speed : int = -300
    var gravity : int = ProjectSettings.get_setting("physics/2d/default_gravity")
    func _physics_process(delta):
    #gravity
    if not is_on_floor():
    velocity.y += gravity * delta

    #Jump
    if Input.is_action_just_pressed("jump") and is_on_floor():
    velocity.y = jump_speed

    #Input Direction
    var direction = Input.get_axis("move_left","move_right")
    if direction:
    velocity.x = direction * speed
    else:
    velocity.x = move_toward(velocity.x, 0, speed)

    move_and_slide()

    • @reric88
      @reric88 11 หลายเดือนก่อน +1

      Man, thanks. I've had so much trouble in the past with a lot of different software where the dev's changed the syntax of the code and moved stuff, it's such a headache. Your solution (copied and pasted) didn't work for me, but I was able to use parts of it and work my way through it comparing the differences between yours and the video.
      For anyone else wanting a different version of the code, here is what I came up with for Godot 4:
      extends CharacterBody2D
      var spd : int = 0
      var jump_spd : int = 200
      var gravity : int = ProjectSettings.get_setting("physics/2d/default_gravity")
      var height : int = 200
      func _physics_process(delta):
      # set speed of player
      # I will reference spd and SPEED. spd is referencing the variable (positive or negative)
      # whereas SPEED will always represent the actual movement speed (always positive)
      velocity.x = spd
      # move right
      if Input.is_physical_key_pressed(KEY_D):
      # if key is pressed, increase right SPEED by increments of 5 per tic until 200,
      # then stay at 200
      spd += 5
      if spd >= 200:
      spd = 200
      # move left
      if Input.is_physical_key_pressed(KEY_A):
      # if key is pressed, increase left SPEED by increments of 5 per tic until 200,
      # then stay at 200 (but is a negative value so it knows to move left)
      spd -= 5
      if spd 0:
      # if key is not pressed and spd value is greater than 0 (to prevent activation
      # while moving left), decrease SPEED by 6 per tic
      spd -= 6
      if spd < 4 && spd > -4 && not Input.is_physical_key_pressed(KEY_A) && not Input.is_physical_key_pressed(KEY_D):
      # if speed is between 4 and -4 and no direction is pressed, spd variable = 0
      # (to prevent unwanted movement while no key is pressed.)
      # I chose 4 and -4 so the spd doesn't bounce between 6 and -6 from above
      spd = 0
      # gravity
      if not is_on_floor():
      # if not currently jumping
      velocity.y += gravity * delta
      # jump
      if Input.is_action_just_pressed("jump") && is_on_floor():
      # if jump is pressed and player is currently on the floor
      if spd > 100:
      # if the spd is greater than 100, multiply spd by 1.7
      # (to increase jump height by your speed), and subtract that from velocity.y
      # this is for right movement, because you need a negative number to go up
      # (negative y is up on the screen)
      velocity.y -= spd * 1.7
      if spd < -100:
      # if the spd is less than -100, multiply spd by 1.7
      # and subtract it from velocity.y (to remain negative)
      velocity.y += spd * 1.7
      if spd > -100 && spd < 100:
      # default jump value
      velocity.y -= jump_spd
      # run the movement function
      move_and_slide()

  • @anthonypagnotta3034
    @anthonypagnotta3034 ปีที่แล้ว +11

    For anyone else who wants to use it, working copy pastable version of final code:
    extends KinematicBody2D
    var speed : int = 400
    var jump_speed : int = 200
    var gravity : int = 200
    var velocity = Vector2()
    #This is a fuction for player movement
    func get_input(delta):
    velocity.x = 0

    #This is for the player to move right
    if Input.is_action_pressed("move_right"):
    velocity.x += speed
    #This is for the player to move left
    if Input.is_action_pressed("move_left"):
    velocity.x -= speed
    #This is for the player to jump
    if Input.is_action_just_pressed("jump"):
    #Checks to see if player is on the ground or not
    if ( is_on_floor()):
    velocity.y -=jump_speed


    # gravity
    velocity.y += gravity * delta
    velocity = move_and_slide(velocity, Vector2.UP)
    pass
    #Process the physics?
    func _physics_process(delta):
    get_input(delta)

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

      always make sure to use the Documentation to study the code! and do try to get used to typing it out yourself now and then!

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

    loved it. But next time can you increase your UI size a bit so that we can see clearly

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

    Thanks for this - was useful in getting me started again. It's worth saying that this TileMap is clearly a 16x16 map, hence why your elements don't line up and are also considerably different in scale to your character.

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

    I think this video is more about the overall process, and not necessarily a tutorial with all the hand-holding on how to do it exactly.

  • @carllrac8370
    @carllrac8370 2 ปีที่แล้ว +18

    Can you make a tutorial about how to add RNG in the game like it generates something randomly in the world and how to make something infinite like an infinite world?

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

    Couldn't figure out how to get that window open to make a new scene and save it. You used a hotkey and I'm not sure what it was.

  • @leosghoul1917
    @leosghoul1917 2 ปีที่แล้ว +6

    im stuck. i have the identical code to yours but my errors keep piling up. i have looked over it 25 times already. helppppp

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

    Wrote down the code for people who need it + comments :
    However I for some reason couldn't replicate the physics displayed in the video despite following every single step. (though I'm feeling I might of missed something).
    extends KinematicBody2D
    var speed : int = 400
    var jump_speed : int = 200
    var gravity : int = 200
    var velocity = Vector2()
    #This is a fuction for player movement
    func get_input(delta):
    velocity.x = 0

    #This is for the player to move right
    if Input.is_action_just_pressed("move_right"):
    velocity.x += speed
    #This is for the player to move left
    if Input.is_action_just_pressed("move_left"):
    velocity.x -= speed
    #This is for the player to jump
    if Input.is_action_just_pressed("jump"):
    #Checks to see if player is on the ground or not
    if ( is_on_floor()):
    velocity.y -=jump_speed


    # gravity
    velocity.y += gravity * delta
    velocity = move_and_slide(velocity, Vector2.UP)
    pass
    #Process the physics?
    func _physics_process(delta):
    get_input(delta)

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

    Thank you for doing what you do. You are a shepherd.

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

    If you let the game run and don t jump, doesn't at some point the velocity.y is so big that when you press jump the player will jump less or not at all.?

  • @user-kk6ls8rm4c
    @user-kk6ls8rm4c 2 ปีที่แล้ว +17

    not beginner friendly to anybody trying to use this for their very first game lol 4 minutes in crying from frustration

    • @sugnoma667
      @sugnoma667 2 ปีที่แล้ว +10

      This is so true 😂 he started listing stuff I didn't know what it was and was so confused

    • @user-kk6ls8rm4c
      @user-kk6ls8rm4c 2 ปีที่แล้ว +9

      @@sugnoma667 he was clicking through every massive menu in 0.2 seconds flat and i just sat there trying to figure out what buttons he was pressing in 0.5 times speed

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

      @@user-kk6ls8rm4c true..:(

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

      I know this is 3 months old but I recommend you start in the GoDot docs with your first 2d game. It makes it much easier to understand what’s happening.

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

    Subscribed literally right before you said if your subscribed but I’m also a new viewer lol

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

    i.a is getting so powerful that robots started making yt videos ☠💀

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

    Nice tutorial, awesome explanation! it was ideal for getting started with Godot, I had no trouble following these steps

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

    For those wondering where "cell" went in the Inspector, you just need to follow along the video a little further and when sets the tile map to create a tile set, you can then set it to 32

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

      i added a tileset to the tilemap but the cell is not there

  • @aneerudh1712
    @aneerudh1712 2 ปีที่แล้ว +6

    Really good Godot tutorials! Very helpful!
    But dude if you want more people to click ur video, Wth is that thumbnail😂

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

    Thank you very much

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

    Thankyou so much bro.

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

    This doesn't work at all for me. I can't get the game to run at all, and I'm at 13:36. My script doesn't like the () at the end of var velocity : Vector2(), and when I take THAT out, I get "expected end of statement after expression, got : instead" for line 23 "get_input(delta):"
    It just throws it into debug, and I can't really get anything to work. I guess too much was changed in basic functionality between 3.4.x and 3.5.1
    Hell, I don't get ANY of the autocomplete you have. None of it.

    • @pedrobui.669
      @pedrobui.669 ปีที่แล้ว +1

      Hey! You've probably already solved this, bit here it is for anyone else with the same problem:
      For the first issue, you have to understand the difference between " : " and " = ". The first is used to tell godot about the *type* the of the variable. It is completely optional, but really helps with performance, and it's barely a hassle to add them, like how he does in the video with the first three variables. The latter symbol serves to *declare* the variable, to set it's value. Your problem is that, by using the ":", you're telling godot "Hey! The type of this variable is Vector2()!" But Vector2() is not a type: Vector2 is the correct type. So, basically, swap the ":" for a "=";
      For the second issue, you have to take out the ":" at the end. By putting it there, you're basically telling godot: "Hey! Start a function here!", but Godot expects you to call a function, not make a new one. So remove the ":" and you're golden.
      Hope I helped :)

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

    are you use 4k display ı can't read why you not zoom script

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

    Make more videos!! Teach me!!

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

    Can U make a animation video for the character for this?

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

    👍

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

    For some reason nothing loads up when I run it

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

    My player does not jump, but does everything else very well

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

    When you selected the 4th tile at about 4:21 how did you do it? I tried ctrl+clicking but it deselects the previous three. I was only able to select those three by click+dragging.

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

    is there a video explaining how to animate this pack?

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

      with aseprite. its not free but you can make all your own animations with your own creation. if you don't want it so pixel like then use blender that's free and can animate. you just import your animations into the game engine and apply them

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

      @@waclac but isn't it free if you compile it yourself ?

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

      @@keepitshort4208 what do you mean? like compile the animations? i just know for pixel related creations asprite has the tools to make that and make animations for it, that’s not free though. sure you can also use the game engine itself to make creations and animations, but if you don’t know how to do that then blender is free and can help with too with tools it offers. using the game engine itself to do it might be limited idk I never used the engine itself to make creations or animations

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

      @@keepitshort4208 ohhh, yeah in that way you can get aseprote free but idk how you would update it if an update comes out. I’m sure there is lots of places you can get aseprite free if you search in on TH-cam and follow a tutorial on how. same with adobe products and games

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

    asset link plz

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

    Lots of things changed with godot 4 . And unfortunetely made this tutorial kinda unusable.

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

    i did everything he does not jump