Architecture Overview
The player system is divided into four specialized components:- PlayerInput - Handles horizontal movement and character orientation
- PlayerJump - Manages jump mechanics, gravity, wall sliding, and double jumping
- CollisionDetection - Detects ground and wall collisions using overlap checks
- PlayerDeath - Handles death animations via events
This system uses a physics-based approach where gravity is dynamically calculated based on desired jump height and distance, providing consistent and tunable jump arcs.
PlayerInput
Handles horizontal player movement using Unity’s Input System and manages character sprite flipping.Configuration
Movement speed in units per second
Reference to the player’s Animator component for walk animations
Implementation
The movement system usesFixedUpdate() for physics-based movement:
PlayerInput.cs:20-30
Input Handling
Movement input is received through Unity’s Input System:PlayerInput.cs:32-36
Character Flipping
The character sprite automatically flips to face the movement direction:PlayerInput.cs:38-44
PlayerJump
Implements a sophisticated jumping system with physics-based gravity calculation, variable jump height, wall sliding, and double jump mechanics.Configuration
Maximum height the player can reach when jumping
Time in seconds the jump button must be held to reach maximum height
Horizontal distance traveled to reach jump apex
Horizontal speed used for gravity calculations
Maximum falling speed when sliding down a wall
Reference to the player’s Animator for jump animations
Physics-Based Gravity
The system calculates gravity dynamically based on desired jump characteristics:PlayerJump.cs:96-100
Jump Force Calculation
PlayerJump.cs:107-110
Variable Jump Height
Players can control jump height by holding or releasing the jump button:PlayerJump.cs:69-73
Jump Initiation
PlayerJump.cs:49-67
Double jump has a 0.2-second delay after the initial jump to prevent accidental activation.
Wall Sliding
When touching a wall, the player’s fall speed is limited:PlayerJump.cs:91-94
Jump Events
The system broadcasts jump events for other systems to react:PlayerJump.cs:26
PlayerJump.cs:75-81
CollisionDetection
Detects ground and wall collisions using circular overlap checks at specific transform positions.Configuration
Layer mask defining which layers count as ground or walls
Transform position where ground detection is performed (typically below the player)
Transform position where wall detection is performed (typically in front of the player)
Ground Detection
CollisionDetection.cs:29-33
Wall Detection
CollisionDetection.cs:35-39
Public API
Other components query collision state through these methods:CollisionDetection.cs:41-49
Collision checks run in
FixedUpdate() to stay synchronized with Unity’s physics system.PlayerDeath
Handles player death animations through an event-driven system.Configuration
Reference to the player’s Animator for triggering death animations
Event Subscription
PlayerDeath.cs:7-15
Death Animation
PlayerDeath.cs:17-20
The death system uses events to decouple death logic from animation, allowing the game state to trigger death from various sources (falling, enemies, hazards) without tight coupling.
Integration Example
All four components work together on the player GameObject:Best Practices
-
Tune jump parameters together -
JumpHeight,DistanceToMaxHeight, andSpeedHorizontalare interdependent. Adjust them as a set for consistent feel. -
Layer mask setup - Ensure the
groundLayerin CollisionDetection includes all surfaces the player should interact with. -
Check point positioning - Position
groundCheckPointslightly below the player’s collider andfrontCheckPointat mid-height on the player’s front edge. -
Physics settings - Use
FixedUpdate()for all physics modifications to avoid frame-rate dependent behavior. -
Event cleanup - Always unsubscribe from events in
OnDisable()to prevent memory leaks.