Godot Version
4.3
Question
In _physics_process I want to update ray.target_position and recalculate collisions. But it does not give me the correct collision information.
I use the following code:
var ray = $Camera3D/RayCast3D
var a = ray.global_position
var b = get_node("../pickable/target_for_ray").global_position
ray.target_position = b - a
ray.force_raycast_update()
if ray.is_colliding():
...
Background info: I want to check if an object is in line of sight of my player.
As per https://www.reddit.com/r/godot/comments/16w5hqq/3d_raycast_not_working_correctly/
Your raycast is a child of the camera3d and is moving constantly. the target_position is local to the raycast so it will not hit the target as expected.
Also, what do you mean by the “correct collision information”? What are the results you are expecting and what results are you getting?
See also Ray-casting — Godot Engine (stable) documentation in English which contains an example snippet that seems to align with your usecase.
Thanks for the reply. I solved the problem, it was similar to the topic you mentioned. The problem was that I did not consider the rotation of the ray.
Working code:
var ray = $Camera3D/RayCast3D
var b = get_node("../pickable/target_for_ray").global_position
ray.target_position = ray.to_local(b)
ray.force_raycast_update()
if ray.is_colliding():
1 Like