🎮

First Person Character Controller Tutorial

Jul 17, 2024

First Person Character Controller Tutorial

Introduction

  • Topic: Making a complete first-person character controller from scratch.
  • Features: Includes head bob, inertia, and movement FOV change.

Setting Up the Scene

  • New Scene: Create a 3D scene called world.
  • Ground: Use CSGBox node for the ground.
  • Character: Use CharacterBody3D node with a bean shape mesh and a simplified convex collision shape.
  • Optimize Performance: Turn off mesh visibility for better performance.

Script Setup

  • Default Template: Add a script to the character body with default movement and jumps.
  • Environment Setup: Add WorldEnvironment and DirectionalLight to the scene tree for lighting.
  • Camera Setup: Add a camera as a child of a Node3D (pivot) to avoid janky rotations.

Basic Movement and Jumping

  • Define constants: speed, jump_velocity, and gravity.
  • Handle jumping: Set vertical velocity when space is pressed; adjust it every tick.
  • Use move_and_slide function to update character position based on velocity & collisions.
  • Convert input directions to Vector3 and multiply by character facing direction for movement.
  • Add friction for realistic slowing down (note: fix friction to avoid a sudden stop).

Input Mapping and Cursor Control

  • Custom Input Map: Define custom actions in Project Settings and bind them.
  • Hide Cursor: Capture the mouse cursor in the _ready function.
  • Node References: Save references to head and camera in onready variables.
  • Camera Rotation: Use unhandled_input to update camera rotation based on mouse movement.
    • Rotate head on y-axis for mouse X-movement.
    • Rotate camera on x-axis for mouse Y-movement.
  • Use the clamp function to limit camera rotation for a better experience._

Enhancing Game Feel (Game Juice)

Basic Adjustments

  • Adjust movement direction to be based on where the head is facing.
  • Better visual presentation: Add more CSGBox nodes with different colors for a map.

Head Bob Effect

  • Purpose: Simulate character's footsteps by moving the camera up and down.
  • Mathematics: Use sine wave for vertical head bob; optionally add horizontal bob with cosine wave.
  • Variables Needed:* bob_frequency, bob_amplitude, and a variable to track progress along the sine wave.
  • Implementation: In each physics tick, update head bob based on speed and ground collision.

Sprinting

  • Speed Variables: Define walk_speed and sprint_speed constants.
  • Switch Speed: Adjust speed variable based on whether the shift key is pressed.

Inertia

  • Problem: Instant stop in midair when movement keys are released.
  • Solution: Use lerp function to interpolate velocity, providing smoother transitions.

FOV Change

  • Variables Needed: base_fov, fov_change.
  • Implementation: Adjust FOV based on clamped velocity to avoid extreme changes when falling.
  • Smooth Transition: Again, use lerp function to smoothly change FOV every physics tick.

Conclusion

  • Project Link: The GitHub project link is available for further exploration.
  • Next Steps: Open to adding specific features if there's enough interest.
  • Feedback: Like the video and leave comments for further suggestions.