Skip to content

Setting up our player

Creating the Player

Great! Let’s start by making a basic version of our player character that will let us move around. We’ll worry about more complicated things like attacking later.

  1. Start by creating a new 2D Scene, with a CharacterBody2D as the root node. Call it ‘Player’

  2. Give it two children: A CollisionShape2D and an AnimatedSprite2D

Here’s how my scene looks with no other modifications:

Basic player scene

Let’s hit Ctrl + S and save this scene in our Scenes folder, call it Player.tscn

Animations

Let’s give ourselves something to look at!

  1. Click on the AnimatedSprite2D and in the inspector, under Animation you’ll see <Empty> in the Spriteframes field.

  2. Click on <Empty> and create a new Spriteframes Click on the Spriteframes you created. This will open a new window at the bottom of the screen. This is where we’ll create our player’s animations. Rename the General animation to Idle and click the Add frames from File Button (The folder icon)

  3. Navigate to your assets/frames folder, and decide which character you want to be your player. I’ll be using the Plague Doctor. Using Shift + Click select all the frames for your character labeled Idle (This should be four frames) then open them.

    Idle Animation Frames

    You’ll see them added to the animation timeline.

  4. We’ll want to select two things in the timeline. The Loop Button (To ensure the animation loops) and the Play on start button (To ensure the animation plays automatically) Let’s also increase the FPS to 8 so that the animation plays a little faster.

    Idle Animation Frames

    Hit play to test! You’ll see the player now has an idle animation that loops!

  5. Let’s add our character’s walking animation. Add a new animation using the Add animation Button and call it “Walk”

    Idle Animation Frames

    Do the same thing we did to grab the frames for the idle animation, but this time, grab all the frames labeled “Run” it should again be 4 frames. We want this animation to loop, but we don’t want it to autoplay. Let’s also give this a framerate of 8 FPS.

    Walk Animation Frames

    Finally, just select your idle animation again, to make sure this is what our player will start on.

    Great! That’s our animations all done!

Collision

Now that we have something to look at, let’s give our player a hitbox.

  1. Open the inspector for the CollisionShape2D we added, and add a new shape in the empty shape field. It’s a good idea to use a CapsuleShape as it will make us less likely to get stuck on corners. Position and adjust the capsule so that it’s slightly smaller than the sprite for our player. Mine looks like this:

    CollissionShape

  2. Great! Our player now has collision. We’ll do one more thing while we’re here, which is give our player a script to handle movement. Right click on the CharacterBody2D and Attach a script. Call it something like “player.gd” and make sure we’re saving it in our Scripts folder. We also need to make sure we untick the template box as we will not be using the template! This is because the template is designed for gravity based platformers.

With our script created and attached, let’s get to programming our movement!

Movement

  1. First, let’s set up a variable to control our speed.

    extends CharacterBody2D
    @export var speed = 200

    The @export tag will allow us to easily edit our speed variable, without needing to open the script!

  2. Then, we’ll want to use Godots built-in _physics_process(delta): function for our movement logic. Inside that we’ll want to get the combined vector of all the inputs the player is pressing.

    func _physics_process(delta):
    var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  3. We’ll then add some lines to multiply this vector by our speed, and then invoke Godots built-in move_and_slide() function, which actually does the moving!

    func _physics_process(delta):
    var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
    velocity = direction * speed
    move_and_slide()
  4. giving us a final script that looks like this:

    extends CharacterBody2D
    @export var speed = 200
    func _physics_process(delta):
    var direction = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
    velocity = direction * speed
    move_and_slide()

For now our movement is controlled using the arrow keys, but we’ll go over how to map it to whatever we want later in the guide.

Switch back to 2D view at the top of the screen.

And that’s our player ready to go for now!

Checklist

  • I’ve created the player scene
  • I’ve setup the animations
  • I’ve attached the script