Problems with Camera Raycast3D

Godot Version

v4.2.1.stable.official [b09f793f5]


Hello! How are you, what happens is that the Raycast that I create and position using the same code as the one in the Godot Docs Raycast section:

# Godot Docs Version:
const ray_length = 1000

func _input(event):
    if event is InputEventMouseButton and event.pressed and event.button_index == 1:
          var camera = $Camera
          var from = camera.project_ray_origin(event.position)
          var to = from + camera.project_ray_normal(event.position) * ray_length

# My Version:
         var MousePos = get_viewport().get_mouse_position()
         var From = GameCamera.project_ray_origin(MousePos)
         var To = From + GameCamera.project_ray_normal(MousePos) * 1000
         $Main3D/RayCast3D.global_position = From
         $Main3D/RayCast3D.target_position = To

but the Raycast would seem displaced if I move the camera, why would that happen to me?, thanks!


I suspect that the box is somehow I child of the camera?

I don’t think it’s a ray cast issue because once you stop the mouse the ray will not be updated. This is because it is waiting for input to cast the ray.

no, the box is not connected to the camera as a node

I already solved it with the solution that this Discord thread posted:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.