raycast method

RayHit? raycast(
  1. Ray ray, {
  2. double maxDistance = double.infinity,
  3. LayerMask layers = LayerMask.all,
})

The closest object along ray, no further than maxDistance, or null when it hits nothing. It reads the world as it is now, not as the last frame showed it, so it finds what the game just moved.

It hits what a view draws: objects with a Transform and a MeshRenderer with both a mesh and a material, from the side they're drawn from (both sides for a Material.doubleSided one), each part (Mesh.submeshCount) only with a material of its own and from its own material's sides, and a skinned character where its skeleton has bent it to. Cut-outs (Material.alphaCutoff) are hit as if solid, and only objects on layers are hit (GameObject.layer): the ray goes through the rest. No physics is involved: each object's box is tested, then the triangles of those the ray may hit.

Implementation

RayHit? raycast(Ray ray, {double maxDistance = double.infinity, LayerMask layers = LayerMask.all}) {
  _sendTransforms();
  final r = scratch.ray.ref;
  r.origin.write(ray.origin);
  r.direction.write(ray.direction);
  check(fy_raycast(pointer, scratch.ray, maxDistance, layers.value, scratch.rayHit));
  final hit = scratch.rayHit.ref;
  if (hit.object == 0) return null;
  return RayHit(_object(hit.object), hit.point.read(), hit.normal.read(), hit.distance);
}