Godot Version
Gd4
Question
can yall pls help:
extends CharacterBody3D
#Movement Controls
var speed
const WALK_SPEED = 5.0
const SPRINT_SPEED = 8.0
const JUMP_VELOCITY = 4.5
const SENSITIVITY = 0.005
#Headbobbing Controls
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
var t_bob = 0.0
#FOV Controls
const BASE_FOV = 75.0
const FOV_CHANGE = 1.5
#Interaction Controls
@export var interact_distance : float = 2
var interact_cast_result
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = 9.8
@onready var Head = $Head
@onready var Camera = $Head/Camera3D
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
#Camera
func _unhandled_input(event):
if event is InputEventMouseMotion:
Head.rotate_y(-event.relative.x * SENSITIVITY)
Camera.rotate_x(-event.relative.y * SENSITIVITY)
Camera.rotation.x = clamp(Camera.rotation.x, deg_to_rad(-40), deg_to_rad(60))
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("Jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
if Input.is_action_pressed("Sprint") and is_on_floor():
speed = SPRINT_SPEED
else:
speed = WALK_SPEED
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("Move_Left", "Move_Right", "Move_Forward", "Move_Backward")
var direction = (Head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if is_on_floor():
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
t_bob += delta * velocity.length() * float(is_on_floor())
Camera.transform.origin = _HeadBob(t_bob)
var velocity_clamped = clamp(velocity.length(), 0.5, SPRINT_SPEED * 2)
var target_fov = BASE_FOV + FOV_CHANGE * velocity_clamped
Camera.fov = lerp(Camera.fov, target_fov, delta * 8.0)
func _process(_delta):
move_and_slide()
#Headbob
func _HeadBob(time) → Vector3:
var pos = Vector3.ZERO
pos.y = sin(time * BOB_FREQ) * BOB_AMP
pos.x = cos(time * BOB_FREQ / 2) * BOB_AMP
return pos
func interact_cast() → void:
var _camera = Global.Player.Head
func interact() → void:
pass
How do i Get Gobal in the current scope