Godot Behavior Tree & Navigation Tutorial (P1 - Player Control)

4 min read 7 months ago
Published on Aug 15, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will walk through the process of setting up a simple player control system using the Godot engine. You'll learn to create a new project, import assets, and script player movement. This tutorial is part of a series focused on implementing AI using behavior trees in Godot, making it a valuable resource for aspiring game developers.

Step 1: Create New Project

  1. Open the Godot engine.
  2. Click on "New Project."
  3. Enter your project name and select a directory for it.
  4. Choose the "2D" option since we are making a top-down shooter.
  5. Click "Create & Edit" to start your new project.

Step 2: Set Up Project Structure and Settings

  1. Navigate to the "Project" menu.
  2. Select "Project Settings" to adjust the settings.
  3. Ensure the display settings are appropriate for a 2D game.
  4. Set the width and height (e.g., 800x600) under "Display > Window."

Step 3: Download Asset from Kenney

  1. Go to the Kenney.nl website and find the "Top-down shooter" asset pack.
  2. Download the asset pack to your computer.

Step 4: Import Asset into Godot

  1. Open your Godot project.
  2. Click on the "FileSystem" panel.
  3. Right-click and select "Open Folder" to locate your asset folder.
  4. Drag and drop the downloaded asset folder into the Godot "FileSystem" panel.

Step 5: Create Player Scene

  1. In the "Scene" menu, select "New Scene."
  2. Add a "KinematicBody2D" node and rename it to "Player."
  3. Add a "Sprite" node as a child of the Player node.
  4. Set the texture of the Sprite to the player character's image from the imported assets.

Step 6: Configure Pixel Art Texture Settings

  1. Select the Sprite node.
  2. In the "Inspector" panel, adjust the texture settings:
    • Check "Filter" to "Nearest" for pixel art.
    • Uncheck "Mipmaps."

Step 7: Create Player Script

  1. With the Player node selected, click the "Attach Script" button.
  2. Name the script "Player.gd" and click "Create."
  3. This script will control player movement and behavior.

Step 8: Set Up Input Mapping

  1. Go to "Project" > "Project Settings" > "Input Map."
  2. Add new actions for movement (e.g., "move_up," "move_down," "move_left," "move_right").
  3. Assign keys to each action (e.g., W, A, S, D).

Step 9: Implement Player Movement Script

  1. Open the Player.gd script and write the following code:
extends KinematicBody2D

var speed = 200
var velocity = Vector2()

func _process(delta):
    velocity = Vector2()

    if Input.is_action_pressed("move_up"):
        velocity.y -= 1
    if Input.is_action_pressed("move_down"):
        velocity.y += 1
    if Input.is_action_pressed("move_left"):
        velocity.x -= 1
    if Input.is_action_pressed("move_right"):
        velocity.x += 1

    velocity = velocity.normalized() * speed
    move_and_slide(velocity)

Step 10: Understand Godot 2D Coordinate System

  • Remember that the Y-axis in Godot increases as you move down the screen, which is opposite to standard Cartesian coordinates.

Step 11: Max Speed and Velocity Control

  • Adjust the speed variable in the script to manage how fast the player moves.
  • Normalize the velocity to ensure consistent movement speed in any direction.

Step 12: Incorporate Delta Time in Movement

  • Use the delta parameter in the _process() function for frame rate-independent movement, ensuring smooth gameplay.

Step 13: Implement Player Rotation Based on Velocity

  • Add the following line to the _process(delta) method to rotate the player based on movement direction:
if velocity != Vector2():
    rotation = velocity.angle()

Step 14: Add Zero Input Vector Check

  • Implement a check to stop movement when no input is detected by modifying the input handling logic.

Conclusion

You've successfully set up a player control system in Godot using KinematicBody2D, scripted movement based on input, and adjusted for pixel art settings. In the next part of this tutorial series, we'll explore implementing behavior trees for AI control. Continue experimenting with player mechanics and explore other features in Godot to enhance your game development skills!