Creating a character controller in Godot using C# that behaves similarly to the Source Engine (used in games like Half-Life 2 and Portal) involves several key components: player movement, jump mechanics, crouching, and collision detection. Below is a basic outline of how you could implement this in Godot using C#.
1. Setup Your Scene
First, create a KinematicBody3D node, which will serve as the base for your character controller. Add the following child nodes:
CollisionShape3D (for defining the character's collision shape, typically a capsule)
Camera3D (optional, if you want a first-person view)
RayCast3D (optional, for ground checking)
2. Script the Character Controller
Attach a C# script to the KinematicBody3D node.
csharp
using Godot;
using System;
public class SourceEngineController : KinematicBody3D
{
// Movement parameters
private const float Gravity = -9.81f;
private const float JumpForce = 10.0f;
private const float Speed = 7.0f;
private const float AirControl = 0.2f;
private const float GroundFriction = 8.0f;
private const float AirFriction = 0.5f;
// Crouching parameters
private const float CrouchSpeed = 3.5f;
private const float CrouchHeight = 1.0f;
private const float StandHeight = 2.0f;
// State variables
private Vector3 _velocity = Vector3.Zero;
private bool _isJumping = false;
private bool _isCrouching = false;
private CollisionShape3D _collisionShape;
public override void _Ready()
{
_collisionShape = GetNode<CollisionShape3D>("CollisionShape3D");
}
public override void _PhysicsProcess(float delta)
{
// Handle input
Vector3 direction = GetDirection();
bool jump = Input.IsActionJustPressed("jump");
// Handle crouching
if (Input.IsActionPressed("crouch"))
{
StartCrouch();
}
else
{
StopCrouch();
}
// Apply gravity
_velocity.y += Gravity * delta;
// Handle ground movement
if (IsOnFloor())
{
_velocity = MoveGround(direction, _velocity, delta);
// Handle jumping
if (jump && !_isJumping)
{
_velocity.y = JumpForce;
_isJumping = true;
}
}
else
{
// Handle air movement
_velocity = MoveAir(direction, _velocity, delta);
}
// Apply movement
_velocity = MoveAndSlide(_velocity, Vector3.Up);
// Check if player is on the ground
if (IsOnFloor())
_isJumping = false;
}
private Vector3 GetDirection()
{
Vector3 direction = Vector3.Zero;
if (Input.IsActionPressed("move_forward"))
direction -= Transform.basis.z;
if (Input.IsActionPressed("move_backward"))
direction += Transform.basis.z;
if (Input.IsActionPressed("move_left"))
direction -= Transform.basis.x;
if (Input.IsActionPressed("move_right"))
direction += Transform.basis.x;
return direction.Normalized();
}
private Vector3 MoveGround(Vector3 direction, Vector3 velocity, float delta)
{
// Apply friction
velocity = velocity.LinearInterpolate(Vector3.Zero, GroundFriction * delta);
// Calculate movement
Vector3 move = direction * Speed;
velocity.x = move.x;
velocity.z = move.z;
return velocity;
}
private Vector3 MoveAir(Vector3 direction, Vector3 velocity, float delta)
{
// Apply friction
velocity = velocity.LinearInterpolate(Vector3.Zero, AirFriction * delta);
// Calculate air control
Vector3 move = direction * Speed * AirControl;
velocity.x += move.x * delta;
velocity.z += move.z * delta;
return velocity;
}
private void StartCrouch()
{
if (!_isCrouching)
{
_isCrouching = true;
_collisionShape.Scale = new Vector3(_collisionShape.Scale.x, CrouchHeight / StandHeight, _collisionShape.Scale.z);
}
}
private void StopCrouch()
{
if (_isCrouching)
{
_isCrouching = false;
_collisionShape.Scale = new Vector3(_collisionShape.Scale.x, StandHeight / CrouchHeight, _collisionShape.Scale.z);
}
}
}
3. Explanation of Key Components
Gravity and Jumping: Basic gravity is applied to the character. Jumping is handled by checking if the player is on the ground and then applying an upward force.
Movement: Movement on the ground is straightforward. The velocity is calculated based on input and then applied. When in the air, there's less control (air control) over movement which is a common mechanic in the Source Engine.
Friction: Different levels of friction are applied depending on whether the player is on the ground or in the air, which affects how quickly the player can stop or change direction.
Crouching: When the crouch button is pressed, the character's collision shape is scaled down to simulate crouching. The movement speed is also reduced.
4. Input Mapping
Make sure to set up the input actions in Godot’s Input Map:
move_forward (W key)
move_backward (S key)
move_left (A key)
move_right (D key)
jump (Spacebar)
crouch (Ctrl or C key)
5. Tuning and Testing
Adjust the constants like Speed, JumpForce, and AirControl to match the exact feel you want, similar to Source Engine games.
Test the controller in various scenarios (on slopes, while jumping, etc.) to ensure it feels responsive and behaves correctly.
This is a basic implementation that covers the core aspects of a Source Engine-like character controller. You can extend it with more features such as sprinting, sliding, and more advanced collision handling to suit your game's needs.
Cite this conversation
“how can I crate "source engine" like character controller in godot using c#.” Conversation with chatgpt-4o-latest, from the LMArena Human Preference 100K dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m850gqj8qr00psa781ds5vw6