proper horizontal speed variable so acceleration can exist

This commit is contained in:
Haze Weathers 2025-03-14 12:35:59 -04:00
parent c90da1eda2
commit c91c931ad3
2 changed files with 18 additions and 3 deletions

View file

@ -1,13 +1,18 @@
extends CharacterBody3D
@export var walk_acceleration: float
@export var walk_speed: float
@export var stopping_rate: float
@export var gravity: float
@export var jump_force: float
@export var level_path: Path3D
var h_speed: float = 0.0
func _physics_process(delta: float) -> void:
# snap player horizontally to path
var path_offset = level_path.curve.get_closest_offset(level_path.to_local(global_position))
@ -18,9 +23,15 @@ func _physics_process(delta: float) -> void:
global_transform.basis.z = -path_trans.basis.z
# horizontal movement
var h_movement = global_transform.basis.z * Input.get_axis(&"walk_left", &"walk_right") * walk_speed
velocity.x = h_movement.x
velocity.z = h_movement.z
var input_dir = signf(Input.get_axis(&"walk_left", &"walk_right"))
h_speed += input_dir * walk_acceleration * delta
if input_dir == 0.0:
h_speed = move_toward(h_speed, 0.0, stopping_rate * delta)
h_speed = clampf(h_speed, -walk_speed, walk_speed)
var h_velocity = global_transform.basis.z * h_speed
velocity.x = h_velocity.x
velocity.z = h_velocity.z
# vertical movement
velocity.y -= gravity * delta
@ -28,3 +39,5 @@ func _physics_process(delta: float) -> void:
velocity.y = jump_force
move_and_slide()
# update horizontal speed after slide

View file

@ -9,7 +9,9 @@ radius = 0.25
[node name="Player" type="CharacterBody3D"]
script = ExtResource("1_g7ett")
walk_acceleration = 18.0
walk_speed = 6.0
stopping_rate = 24.0
gravity = 15.0
jump_force = 8.0