Moving left and right and jumping of Player is an Action. We can use these actions by assigning a key to them.
To define Actions, follow the steps below.
1. Select the "Input Map" tab from the "Project – Project Settings..." menu option.
2. Enter each value below in order in the "Add New Action" box and press ENTER.
3. Click the + button to the right of the "jump" line. While in the box at the top of the window that opens, select the Space key by pressing the Space key and click the "OK" button.
4. Repeat the same process twice for the "move_left" line and select the left arrow and A keys.
5. Repeat the same process twice for the "move_right" line, select the right arrow and D keys and click the "Close" button.
6. Edit the "player.gd" script code as follows to use the functions we created instead of the default functions of the Godot game engine.
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
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()
7. To update the player graphics and make them face the direction they are moving, while the "player" Scene is selected, drag and drop the "AnimatedSprite2D" Node onto the line where the _physics_process() function is defined. Hold down the "Ctrl" key before dropping. This process creates a variable. edit the "player.gd" script code as follows so that the Player's face is facing the direction he is moving.
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# Get the input direction: -1, 0, 1
var direction = Input.get_axis("move_left", "move_right")
# Flip the Sprite
if direction > 0:
animated_sprite_2d.flip_h = false
elif direction < 0:
animated_sprite_2d.flip_h = true
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
8. Switch to 2D display mode with the "2D" button on the top line of Godot IDE.
9. While the "player" Scene and "AnimatedSprite2D" Node are selected, create two animations named "jump" and "run" with the + button under "Animations".
10. While "run" is selected, select the "knight.png" file in the "assets/sprites" directory with the "Add frames from sprite sheet" button.
11. In the window that appears, make the settings as follows and click the "Add 16 Frame(s)" button.
12. do the same operations while "jump" is selected, and in the window that appears, make the settings as follows and click the "Add 1 Frame(s)" button.
13. Set animations for idle, running and jumping by editing the script as follows.
extends CharacterBody2D
const SPEED = 130.0
const JUMP_VELOCITY = -300.0
@onready var animated_sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# Get the input direction: -1, 0, 1
var direction = Input.get_axis("move_left", "move_right")
# Flip the Sprite
if direction > 0:
animated_sprite_2d.flip_h = false
elif direction < 0:
animated_sprite_2d.flip_h = true
# Play animations
if is_on_floor():
if direction == 0:
animated_sprite_2d.play("idle")
else:
animated_sprite_2d.play("run")
else:
animated_sprite_2d.play("jump")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
Save all scenes with Ctrl-S keys.