You need to send the raycast out into the physics world, that is why its not working. You will need to get the space-state and then send the raycast. Since you are getting the input in the _input
function, you'll want to cache it so you can use it in _physics_process
.
I believe something like this should work like you are expecting:
private Camera camera;
private const float rayLength = 1;
private bool do_raycast = false;
private Vector3 ray_origin = Vector3.Zero;
private Vector3 ray_end = Vector3.Zero;
public override void _Input(InputEvent @event)
{
if (camera == null)
camera = PlayerController.Instance.camera;
if (@event is InputEventMouseButton eventMouseButton && eventMouseButton.Pressed && eventMouseButton.ButtonIndex == 1)
{
Transform holder = new Transform();
holder.origin = GlobalTransform.origin;
GlobalTransform = holder;
Vector3 from = camera.ProjectRayOrigin(eventMouseButton.Position);
Vector3 to = from + camera.ProjectRayNormal(eventMouseButton.Position);
ray_origin = from;
ray_end = to;
do_raycast = true;
}
}
public override void _PhysicsProcess(float delta)
{
if (do_raycast == true)
{
DirectSpaceState space_state = GetWorld().DirectSpaceState;
Godot.collections.dictionary raycast_result = space_state.IntersectRay(
ray_origin, ray_end);
if (raycast_result.Count > 0) {
GD.print(raycast_result)
if (result["collider"] is Tabs) {
result["collider"].OnTab(true);
}
}
do_raycast = false;
}
}