Difference between 2d raycasts

What is the difference between

public static int Raycast(Vector2 origin, Vector2 direction, ContactFilter2D contactFilter, RaycastHit2D[] results, float distance = Mathf.Infinity);

and

public static int RaycastNonAlloc(Vector2 origin, Vector2 direction, RaycastHit2D[] results, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

other than Raycast taking a ContactFilter2D as an argument instead of RaycastNonAlloc taking a layer mask, min depth and max depth?

The NonAlloc/All methods are tentatively deprecated for all 2D physics queries. They are replaced by the same method with no suffix so in this case “Raycast”. This means you can use a single method which has lots of overloads to achieve the same thing all of which do not produce any memory garbage.

The difference is the arguments and the docs tell you exactly what the arguments are. The docs are being updated to mention this and at a later date, the non-alloc/all will be deprecated so you’ll get a compiler warning.

1 Like