Godot Version
Godot 4.5
Question
I’m trying to make an item that will fall out of chest and player can interact with it. So I need for an item to have gravity. The raycast is in the player’s head in the middle of the camera. When I use StaticBody3D type of node everything works, with CharacterBody3D it works as long as I don’t add physics, with RigidBody3D it doesn’t work at all. The interaction prompt blinks for 1 frame and then it disables. Would be really grateful for help, didn’t find anyone encountering the same problem.
Can you paste your code and maybe a screen shot of your rigid body object’s scene tree?
RigidBody3D scene and script(nothing important in the script itself so I include it on screenshot)
Player.gd script that manages the controls, raycast included
extends CharacterBody3D
const JUMP_CUT_MULTIPLIER: float = 0.5
var jump_height
var gravity = 9.8
var sensitivity = 0.005
var walk_speed
var sprint_speed
var speed
#headbob related stuff
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
var t_bob = 0.0
#fov related stuff
var base_fov = 75.0
var fov_change = 1.5
@onready var head = $Head
@onready var camera = $Head/Camera
@onready var see_cast = $Head/SeeCast
func _ready():
jump_height = State.current_stats.jump_height
walk_speed = State.current_stats.move_speed
sprint_speed = State.current_stats.move_speed * 1.5
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
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(-90), deg_to_rad(90))
func _physics_process(delta: float) -> void:
%PickupText.hide()
if see_cast.is_colliding():
var target = see_cast.get_collider()
print(target)
if target.has_method("interact"):
%PickupText.show()
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
else:
velocity.y = 0.0
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = jump_height
if Input.is_action_just_released("jump") and velocity.y > 0:
velocity.y *= JUMP_CUT_MULTIPLIER
if Input.is_action_pressed("sprint"):
speed = sprint_speed
else:
speed = walk_speed
# Get the input direction and handle the movement/deceleration.
var input_dir := Input.get_vector("left", "right", "up", "down")
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 * 2.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 2.0)
#headbob
t_bob += delta * velocity.length() * float(is_on_floor())
camera.transform.origin = _headbob(t_bob)
#fov
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)
move_and_slide()
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
How do you know it’s for 1 frame?
I don’t know, it’s just really short blink, I assumed it. Sorry.
Isn’t the default gravity taking the rigid body down/away?
Well yes, I placed it purposely slightly above the ground to see if gravity works. But it’s in front of the player so your raycast touches it on start. After it falls and even during falling it stops working.
Enable debug display of collision shapes to see what your raycast is doing
1 Like
THANKS I FOUND THE PROBLEM!!! The raycast just doesn’t work with my mouse movement. I need to fix that. When I point it at RigidBody that is placed on proper height it collides with it. Thanks, I need to fix entirely different thing.
1 Like