The documentation is primarily referring to creating Raycasts through code, so the code needs some modifications to work with Raycast nodes.
If the position is (0, 0, 0) then the Raycast probably didn't collide with anything, so it is returning the origin of the scene.
Do you have physics body nodes (like StaticBody, RigidBody, and/or KinematicBody) with CollisionShape nodes setup in the scene? The raycast will can only collide with physics body nodes with collision shapes by default, with options to allow for colliding with Area nodes with collision shapes.
The other issue could be that the raycast is not being updated with the changes in position/direction before the code checks for a result. Another issue could be that the origin of the raycast isn't being set correctly. If I recall, I think camera.project_ray_origin returns a position in world space, which means you'll need to set the Raycast node's position in world space as well.
I have not tested the code, but the following should work, assuming there are physics bodies with collision shapes in the scene:
extends MeshInstance
const ray_length = 100
func _input(event):
if event is InputEventMouseButton and event.pressed and event.button_index == 1:
print (event.global_position)
var raycast = get_node("../Raycast")
var camera = get_node("../Camera")
# Set the origin of the raycast to the origin position calculated
# from the camera.
# You might need to replace event.global_position with event.position...
raycast.global_transform.origin = camera.project_ray_origin(event.global_position)
# Change the Raycast cast_to property so it points in the right direction.
# Because we are using a Raycast node, we do not need to add
# the position of the Raycast node, as the ray's origin will automatically
# be the origin of the Raycast node.
raycast.points_to = camera.project_ray_normal(event.global_position) * ray_length
# This function is not documented, but it tells the Raycast to update its
# transform, which will update it's position, rotation, and scale with
# the latest changes
raycast.force_update_transform()
# Tell the Raycast to send itself into the physics world with the
# latest changes
raycast.force_raycast_update()
# Did the Raycast collide with anything?
if raycast.is_colliding():
print ("Hit something at position: ", raycast.get_collision_point)
else:
print ("Raycast did not hit anything")
Hopefully this helps :smile: