extends Node class_name StateWallRun const FLAG: int = 0 # unused var machine: MovementStateMachine var params: MovementParams: get: return machine.params var elapsed: float = 0.0 func enter(_data: Dictionary = {}) -> void: elapsed = 0.0 machine.register_chain_mechanic("wall_run") machine.player.velocity.y = params.wall_run_vertical_speed func exit() -> void: pass func update(delta: float) -> void: elapsed += delta if elapsed > params.wall_run_duration: machine.wall_normal = Vector3.ZERO machine.switch_to("air") return var vel: Vector3 = machine.player.velocity # Stick to wall vel.y = min(vel.y, params.wall_run_vertical_speed) # Move along wall: project velocity onto wall tangent var wall_tangent := (machine.wall_normal.cross(Vector3.UP)).normalized() if machine.input_dir.y < 0.0: wall_tangent = -wall_tangent var move_dir: Vector3 = wall_tangent * params.wall_run_speed vel.x = move_dir.x vel.z = move_dir.z if machine.input_jump_just_pressed: var jump_vel: Vector3 = vel + (machine.wall_normal * params.wall_run_jump_off_normal) jump_vel.y = params.wall_run_auto_jump_speed machine.player.velocity = jump_vel machine.wall_normal = Vector3.ZERO machine.register_chain_mechanic("wall_jump") machine.switch_to("air") return # Wall cling fall-through if machine.input_dir.length() < 0.1: machine.switch_to("wall_cling") return # Lose wall contact var still_on_wall: bool = machine.wall_normal != Vector3.ZERO and detect_wall(machine.wall_normal).length_squared() > 0.0 if not still_on_wall: machine.wall_normal = Vector3.ZERO machine.register_chain_mechanic("wall_run_off") machine.switch_to("air") machine.player.velocity = vel machine.player.move_and_slide() if machine.player.is_on_floor(): machine.on_ground = true machine.wall_normal = Vector3.ZERO machine.current_jump_count = 0 machine.register_chain_mechanic("wall_run_land") machine.switch_to("ground") func detect_wall(expected_normal: Vector3) -> Vector3: var origin := machine.player.global_position var offset := expected_normal.cross(Vector3.UP).normalized() var space_state := machine.player.get_world_3d().direct_space_state for side in [1.0, -1.0]: var ray_origin := origin + offset * side * params.wall_detect_distance + Vector3.UP * params.wall_ray_up_height var ray_end := origin + offset * side * params.wall_detect_distance - Vector3.DOWN * params.wall_ray_down_height var ray := PhysicsRayQueryParameters3D.create(ray_origin, ray_end) ray.exclude = [machine.player.get_rid()] var hit := space_state.intersect_ray(ray) if not hit.is_empty(): return hit.get("normal", Vector3.ZERO).normalized() return Vector3.ZERO