extends CharacterBody3D # Member variables var walk_speed = 1.0 var g = -9.8 const MAX_SPEED = 5 const JUMP_SPEED = 5 const ACCEL= 8 const DEACCEL= 8 const MAX_SLOPE_ANGLE = 30 @onready var camera = $CSGMesh3D/Camera3D var _paused = false var _noclip = false func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) func _physics_process(delta): var move_dir = Vector3() # Where does the player intend to walk to var cam_xform = camera.get_global_transform() if Input.is_action_pressed("move_forward"): move_dir += -cam_xform.basis.z if Input.is_action_pressed("move_backwards"): move_dir += cam_xform.basis.z if Input.is_action_pressed("move_left"): move_dir += -cam_xform.basis.x if Input.is_action_pressed("move_right"): move_dir += cam_xform.basis.x if not _noclip: move_dir.y = 0 move_dir = move_dir.normalized() var target = move_dir * MAX_SPEED if Input.is_action_pressed("run"): target *= 5 var accel if move_dir.dot(target) > 0: accel = ACCEL else: accel = DEACCEL var new_vel = velocity.lerp(target, accel * delta) velocity.x = new_vel.x velocity.z = new_vel.z if _noclip: velocity.y = new_vel.y if not _noclip: velocity.y += g * delta if is_on_floor() and Input.is_action_pressed("jump"): velocity.y += JUMP_SPEED move_and_slide() $Ball.global_position = camera.global_position - camera.global_transform.basis.z * 2.0 func _input(event): if event.is_action_pressed("pause"): _paused = not _paused if _paused: Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) else: Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) if _paused: return if event.is_action_pressed('noclip'): _noclip = not _noclip if _noclip: $CollisionShape3D.disabled = true else: $CollisionShape3D.disabled = false # Check for mouse motion input if event is InputEventMouseMotion: rotation.y += -event.relative.x * 0.005 camera.rotation.x += -event.relative.y * 0.005 camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-70), deg_to_rad(70)) if event.is_action_pressed("interact"): var space_state = get_world_3d().direct_space_state # use global coordinates, not local to node var query = PhysicsRayQueryParameters3D.create(camera.global_position, $Ball.global_position) # Don't collide with ourselves query.exclude = [self] var result = space_state.intersect_ray(query) if result.has('collider'): var other: Node3D = result.collider if other.has_method('interact'): other.interact(self)