46 lines
1.5 KiB
GDScript
46 lines
1.5 KiB
GDScript
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 velocity_2d: Vector2 = Vector2.ZERO
|
|
|
|
|
|
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))
|
|
var path_trans = level_path.curve.sample_baked_with_rotation(path_offset, true)
|
|
global_position.x = path_trans.origin.x
|
|
global_position.z = path_trans.origin.z
|
|
global_transform.basis.x = -path_trans.basis.x
|
|
global_transform.basis.z = -path_trans.basis.z
|
|
|
|
# horizontal movement
|
|
var input_dir = signf(Input.get_axis(&"walk_left", &"walk_right"))
|
|
velocity_2d.x += input_dir * walk_acceleration * delta
|
|
if input_dir == 0.0:
|
|
velocity_2d.x = move_toward(velocity_2d.x, 0.0, stopping_rate * delta)
|
|
velocity_2d.x = clampf(velocity_2d.x, -walk_speed, walk_speed)
|
|
|
|
# vertical movement
|
|
velocity_2d.y -= gravity * delta
|
|
if is_on_floor() and Input.is_action_just_pressed(&"jump"):
|
|
velocity_2d.y = jump_force
|
|
|
|
# translate 2d movement to 3d
|
|
velocity.x = global_transform.basis.z.x * velocity_2d.x
|
|
velocity.y = velocity_2d.y
|
|
velocity.z = global_transform.basis.z.z * velocity_2d.x
|
|
|
|
# apply movement and translate 3d velocity back to 2d
|
|
move_and_slide()
|
|
var h_vel = (velocity * Vector3(1.0, 0.0, 1.0))
|
|
velocity_2d.x = h_vel.length() * signf(global_transform.basis.z.dot(h_vel))
|
|
velocity_2d.y = velocity.y
|