How did you initalize the hits array? The NonAlloc calls work like this:
// Somewhere in your class:
private RaycastHit[] hits = new RaycastHit[5];
// In your function:
int numHits = Physics.CapsuleCastNonAlloc(transform.position, Top(transform.position), radius, AngleToVector(angle), hits, magnitude)
The number of results it can return is limited by the size of the array you give to it. In this example, it will only return 5 hits at maximum. Also, it will not resize the array (that would also allocate garbage), so it returns an integer so you know how many hits it actually found.
That’s no longer the case s this was fixed, iirc since 2018.3 or similar but check it via the profiler just to be sure. (Btw, Enum.HasFlag would also allocate in standard .Net but not in Unity as we fixed that in our Mono/IL2CPP version so this is not the only case were we fixed some needless allocations).
Enabling the Call Stacks toggle will give you even more insights on where these GC.Alloc calls came from with less profiling overhead. From 2019.3 on this option can be turned on on the fly while profiling any development build, whereas Deep Profiling support needs to be built into a build. Check the documentation for this feature for the details.
I had an issue with it in a project using 2020.1.X a little while ago. It may have been my implementation. The suggestion you made about the call stacks will help me track that down, I just have to remember where it was.
This I did not know, thanks for the insight it will definitely help. Now to go through where I have allocations that I couldn’t figure out where they were coming from.
It might depend on the collection/IEnumerable you re going over and how that implemented GetEnumerator. If you write one of these yourself, make sure it’s returning a struct.