Build a 2D GODOT Platformer in 20 Minutes!
Вставка
- Опубліковано 15 жов 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.
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()
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()
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()
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)
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!
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.
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.
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.
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?
im stuck. i have the identical code to yours but my errors keep piling up. i have looked over it 25 times already. helppppp
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)
the jump speed is -200
You need to use is_action_pressed( not is_action_just_pressed)
loved it. But next time can you increase your UI size a bit so that we can see clearly
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.?
i.a is getting so powerful that robots started making yt videos ☠💀
Thank you for doing what you do. You are a shepherd.
Subscribed literally right before you said if your subscribed but I’m also a new viewer lol
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
i added a tileset to the tilemap but the cell is not there
Thank you very much
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.
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 :)
Thankyou so much bro.
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.
not beginner friendly to anybody trying to use this for their very first game lol 4 minutes in crying from frustration
This is so true 😂 he started listing stuff I didn't know what it was and was so confused
@@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
@@user-kk6ls8rm4c true..:(
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.
are you use 4k display ı can't read why you not zoom script
Can U make a animation video for the character for this?
is there a video explaining how to animate this pack?
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
@@waclac but isn't it free if you compile it yourself ?
@@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
@@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 UA-cam and follow a tutorial on how. same with adobe products and games
Nice tutorial, awesome explanation! it was ideal for getting started with Godot, I had no trouble following these steps
👍
For some reason nothing loads up when I run it
My player does not jump, but does everything else very well
Really good Godot tutorials! Very helpful!
But dude if you want more people to click ur video, Wth is that thumbnail😂
asset link plz
Lots of things changed with godot 4 . And unfortunetely made this tutorial kinda unusable.
i did everything he does not jump