48 lines
1.6 KiB
GDScript
48 lines
1.6 KiB
GDScript
extends Node
|
|
|
|
@export_group("Noclip")
|
|
@export var noclip_speed: float = 15.0
|
|
@export var noclip_sprint_multiplier: float = 3.0
|
|
|
|
var is_noclip: bool = false
|
|
|
|
# Make sure these paths match your actual node setup!
|
|
@export var head_camera: Camera3D
|
|
@export var player_hitbox: CollisionShape3D
|
|
@export var player: Player
|
|
|
|
func toggle():
|
|
is_noclip = !is_noclip
|
|
|
|
# Disable physics collision so you pass through walls
|
|
player_hitbox.disabled = is_noclip
|
|
|
|
# Reset velocity so you don't carry momentum when toggling it on/off
|
|
player.velocity = Vector3.ZERO
|
|
|
|
print("Noclip: ", "ON" if is_noclip else "OFF")
|
|
|
|
func _process_noclip_movement(_delta: float) -> void:
|
|
var input_dir = Input.get_vector("left", "right", "forward", "backward")
|
|
|
|
# Optional: Add keys to fly straight up and down (like Space and Ctrl)
|
|
var vertical_input = Input.get_axis("crouch", "jump")
|
|
|
|
# Calculate direction based on the CAMERA'S rotation, not the body's!
|
|
var cam_basis = head_camera.global_transform.basis
|
|
var direction = (cam_basis * Vector3(input_dir.x, vertical_input, input_dir.y)).normalized()
|
|
|
|
# Calculate speed (allow sprinting while flying)
|
|
var current_speed = noclip_speed
|
|
if Input.is_action_pressed("sprint"):
|
|
current_speed *= noclip_sprint_multiplier
|
|
|
|
# Apply movement instantly (no friction/acceleration smoothing)
|
|
if direction != Vector3.ZERO:
|
|
player.velocity = direction * current_speed
|
|
else:
|
|
player.velocity = Vector3.ZERO
|
|
|
|
# move_and_slide() still works perfectly!
|
|
# Because collision_shape is disabled, it will just glide through everything.
|
|
player.move_and_slide()
|