Skip to content

Commit

Permalink
Improved CharacterBody script templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
yosoyfreeman committed Dec 24, 2024
1 parent fafc073 commit a97dee1
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,40 @@
extends _BASE_


const SPEED = 300.0
const JUMP_VELOCITY = -400.0
# The @export annotation allows a variable to be shown and modified from the inspector.
@export var SPEED: float = 300.0
@export var ACCEL: float = 300.0
@export var JUMP_SPEED: float = -400.0


func _physics_process(delta: float) -> void:
# Add the gravity.
# Handle gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Get the vertical velocity.
var vertical_velocity: Vector2 = velocity.project(up_direction)

# Get the horizontal velocity.
var horizontal_velocity: Vector2 = velocity - vertical_velocity

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
vertical_velocity = up_direction * JUMP_SPEED

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
var input_axis: float = Input.get_axis("ui_left", "ui_right")

# Calculate the intended direction in 2D plane.
var input_direction: Vector2 = Vector2(input_axis, 0).rotated(rotation)

# Calculate the target horizontal velocity.
var target_horizontal_velocity: Vector2 = input_direction * SPEED

# Move the current horizontal velocity towards the target horizontal velocity.
horizontal_velocity = horizontal_velocity.move_toward(target_horizontal_velocity, ACCEL * delta)

# Compose the final velocity.
velocity = horizontal_velocity + vertical_velocity

move_and_slide()
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@

extends _BASE_


const SPEED = 5.0
const JUMP_VELOCITY = 4.5
# The @export annotation allows a variable to be shown and modified from the inspector.
@export var SPEED: float = 5.0
@export var ACCEL: float = 5.0
@export var JUMP_SPEED: float = 4.5


func _physics_process(delta: float) -> void:
# Add the gravity.
# Handle gravity.
if not is_on_floor():
velocity += get_gravity() * delta

# Get the vertical velocity.
var vertical_velocity: Vector3 = velocity.project(up_direction)

# Get the horizontal velocity.
var horizontal_velocity: Vector3 = velocity - vertical_velocity

# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
vertical_velocity = up_direction * JUMP_SPEED

# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
var input_vector: Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down", 0.15)

# Calculate the intended direction in 3D space.
var input_direction: Vector3 = transform.basis.orthonormalized() * Vector3(input_vector.x, 0, input_vector.y)

# Calculate the target horizontal velocity.
var target_horizontal_velocity: Vector3 = input_direction * SPEED

# Move the current horizontal velocity towards the target horizontal velocity.
horizontal_velocity = horizontal_velocity.move_toward(target_horizontal_velocity, ACCEL * delta)

# Compose the final velocity.
velocity = horizontal_velocity + vertical_velocity

move_and_slide()
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,54 @@

public partial class _CLASS_ : _BASE_
{
public const float Speed = 300.0f;
public const float JumpVelocity = -400.0f;

public override void _PhysicsProcess(double delta)
{
Vector2 velocity = Velocity;

// Add the gravity.
if (!IsOnFloor())
{
velocity += GetGravity() * (float)delta;
}

// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
{
velocity.Y = JumpVelocity;
}

// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 direction = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
if (direction != Vector2.Zero)
{
velocity.X = direction.X * Speed;
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
}

Velocity = velocity;
MoveAndSlide();
}
// The [Export] attribute allows a variable to be shown and modified from the inspector.
[Export]
public float Speed = 300.0f;

[Export]
public float Accel = 300.0f;

[Export]
public float JumpSPeed = -400.0f;

public override void _PhysicsProcess(double delta)
{
Vector2 velocity = Velocity;

// Handle gravity.
if (!IsOnFloor())
{
velocity += GetGravity() * (float)delta;
}

// Get the vertical velocity.
Vector2 verticalVelocity = velocity.Project(UpDirection);

// Get the horizontal velocity.
Vector2 horizontalVelocity = velocity - verticalVelocity;

// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
{
verticalVelocity = UpDirection * JumpSpeed;
}

// As good practice, you should replace UI actions with custom gameplay actions.
float inputAxis = Input.GetAxis("ui_left", "ui_right");

// Calculate the intended direction in 2D plane.
Vector2 inputDirection = new Vector2(inputAxis, 0).Rotated(Rotation);

// Calculate the target horizontal velocity.
Vector2 targetHorizontalVelocity = inputDirection * Speed;

// Move the current horizontal velocity towards the target horizontal velocity.
horizontalVelocity = horizontalVelocity.MoveToward(targetHorizontalVelocity, Accel * (float)delta);

// Compose the final velocity.
velocity = horizontalVelocity + verticalVelocity;

Velocity = velocity;
MoveAndSlide();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,54 @@

public partial class _CLASS_ : _BASE_
{
public const float Speed = 5.0f;
public const float JumpVelocity = 4.5f;

public override void _PhysicsProcess(double delta)
{
Vector3 velocity = Velocity;

// Add the gravity.
if (!IsOnFloor())
{
velocity += GetGravity() * (float)delta;
}

// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
{
velocity.Y = JumpVelocity;
}

// Get the input direction and handle the movement/deceleration.
// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 inputDir = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down");
Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
if (direction != Vector3.Zero)
{
velocity.X = direction.X * Speed;
velocity.Z = direction.Z * Speed;
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, Speed);
}

Velocity = velocity;
MoveAndSlide();
}
// The [Export] attribute allows a variable to be shown and modified from the inspector.
[Export]
public float Speed = 5.0f;

[Export]
public float Accel = 5.0f;

[Export]
public float JumpSpeed= 4.5f;

public override void _PhysicsProcess(double delta)
{
Vector3 velocity = Velocity;

// Add the gravity.
if (!IsOnFloor())
{
velocity += GetGravity() * (float)delta;
}

// Get the vertical velocity.
Vector3 verticalVelocity = velocity.Project(UpDirection);

// Get the horizontal velocity.
Vector3 horizontalVelocity = velocity - verticalVelocity;

// Handle Jump.
if (Input.IsActionJustPressed("ui_accept") && IsOnFloor())
{
verticalVelocity = UpDirection * JumpSpeed;
}

// As good practice, you should replace UI actions with custom gameplay actions.
Vector2 inputVector = Input.GetVector("ui_left", "ui_right", "ui_up", "ui_down", 0.15f);

// Calculate the intended direction in 3D space.
Vector3 inputDirection = Transform.Basis.Orthonormalized() * new Vector3(inputVector.X, 0, inputVector.Y);

// Calculate the target horizontal velocity.
Vector3 targetHorizontalVelocity = inputDirection * Speed;

// Move the current horizontal velocity towards the target horizontal velocity.
horizontalVelocity = horizontalVelocity.MoveToward(targetHorizontalVelocity, Accel * (float)delta);

// Compose the final velocity.
velocity = horizontalVelocity + verticalVelocity;

Velocity = velocity;
MoveAndSlide();
}
}

0 comments on commit a97dee1

Please sign in to comment.