Godot Version
`Godot4
Question
Can you guys pls help im over here helping people now i forgot how to solve something as simple as this, i can’t find it in my notes either so yeah:
class_name Player
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):
interact_cast()
# 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
var space_state = camera.get_World().direct_space_state
var screen_center = get_viewport().size / 2
var origin = camera.project_ray_origin(screen_center)
var end = origin + camera.project_ray_normal(screen_center) * interact_distance
var query = PhysicsRayQueryParameters3D.create(origin, end)
query.collide_with_bodies = true
var result = space_state.intersect_ray(query)
var current_cast_result = result.get(“collider”)
print(current_cast_result)
func interact() → void:
pass