using Godot; namespace Gmtk24 { public static class Util { public static RaycastResult? RaycastFromMouse(Camera3D camera, uint mask = 0b11000) { var viewport = camera.GetViewport(); var origin = camera.ProjectRayOrigin(viewport.GetMousePosition()); var normal = camera.ProjectRayNormal(viewport.GetMousePosition()) * 10000; var ray = new PhysicsRayQueryParameters3D { From = origin, To = normal, CollideWithBodies = true, CollisionMask = mask, HitBackFaces = false, HitFromInside = false, }; var results = camera.GetWorld3D().DirectSpaceState.IntersectRay(ray); if (results.ContainsKey("position")) { return new RaycastResult() { Collider = (GodotObject)results["collider"], Position = (Vector3)results["position"], Normal = (Vector3)results["normal"], }; } return null; } } public struct RaycastResult { public Vector3 Position; public Vector3 Normal; public GodotObject Collider; } }