Skip to content

Commit

Permalink
Fix deceleration in CharacterBody 2D and 3D templates
Browse files Browse the repository at this point in the history
The velocity can never be greater than SPEED because direction is
normalized. This means the move_toward calls are currently equivalent to
assigning the velocity components to 0.

Changing SPEED to SPEED * delta makes the friction effect way too small
(the character drifts too much) so we introduce a new deceleration
constant.

The cosmetics regarding zeros written as floating point are unrelated
to the fix itself.
  • Loading branch information
ubitux committed May 27, 2024
1 parent be56cab commit fc6d8e6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extends _BASE_


const SPEED = 300.0
const DECELERATION = 1000.0
const JUMP_VELOCITY = -400.0


Expand All @@ -22,6 +23,6 @@ func _physics_process(delta: float) -> void:
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.x = move_toward(velocity.x, 0.0, DECELERATION * delta)

move_and_slide()
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extends _BASE_


const SPEED = 5.0
const DECELERATION = 12.0
const JUMP_VELOCITY = 4.5


Expand All @@ -19,12 +20,12 @@ func _physics_process(delta: float) -> void:
# 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()
var direction := (transform.basis * Vector3(input_dir.x, 0.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)
velocity.x = move_toward(velocity.x, 0.0, DECELERATION * delta)
velocity.z = move_toward(velocity.z, 0.0, DECELERATION * delta)

move_and_slide()
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public partial class _CLASS_ : _BASE_
{
public const float Speed = 300.0f;
public const float Deceleration = 1000.0f;
public const float JumpVelocity = -400.0f;

public override void _PhysicsProcess(double delta)
Expand Down Expand Up @@ -33,7 +34,7 @@ public override void _PhysicsProcess(double delta)
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, Speed);
velocity.X = Mathf.MoveToward(Velocity.X, 0.0f, Deceleration * (float)delta);
}

Velocity = velocity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public partial class _CLASS_ : _BASE_
{
public const float Speed = 5.0f;
public const float Deceleration = 12.0f;
public const float JumpVelocity = 4.5f;

public override void _PhysicsProcess(double delta)
Expand All @@ -27,16 +28,16 @@ public override void _PhysicsProcess(double delta)
// 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();
Vector3 direction = (Transform.Basis * new Vector3(inputDir.X, 0.0f, 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.X = Mathf.MoveToward(Velocity.X, 0.0f, Deceleration * (float)delta);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0.0f, Deceleration * (float)delta);
}

Velocity = velocity;
Expand Down

0 comments on commit fc6d8e6

Please sign in to comment.