Tuesday, September 5, 2017

unity - Difference between 'max distance' and 'radius' in Physics.SphereCastAll


I needed a way to check for all gameobjects within a set sphere of my player object.


I found Physics.SphereCastAll which will return a RaycastHit[], exactly what I wanted. But, I am having trouble understanding the method signature/docs.


Basically, it's the two things in the title I am confused on right now:


From Unity docs:




  • radius - The radius of the sphere.

  • maxDistance - The max length of the sweep.


Is 'max length of the sweep' a measurement of time (or degrees?)? I would have thought the length would be the same as the radius (or at least you could calculate it from the radius) but it obviously isn't what I think length means.



Answer



SphereCast methods take a sphere and slide it along a line to see where it hits objects along its travel.


The radius parameter is the size of the sphere.


The maxDistance parameter is how far the sphere should travel in the given direction.


To simply check for objects near your player, you want CheckSphere or OverlapSphere / OverlapSphereNonAlloc instead. These methods check for colliders intersecting a given sphere, and give you either a bool result of whether a collision was detected, or an array of the colliders overlapping the radius. They don't try to slide that sphere to find contact positions.


This is not equivalent to a SphereCast with a maxDistance of 0. Like a raycast, this will only detect the points where it begins to intersect with an object - not situations where it was already overlapping in its starting position.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...