- Home /
Physics.SphereCastNonAlloc returning incorrect information.
Physics.SphereCastNonAlloc is returning incorrect information.
int count = Physics.SphereCastNonAlloc(transform.position, radius, moveVector.normalized, collisions, 1, layerMask);
The hit info that this populates the collision list with is returning incorrect normals and points. It's returning the inverse of my moveVector as the normal and [0, 0, 0] as the point. I've been struggling with this for hours and can't seem to find an answer to my problem.
What is the expected output of this? Be aware that all physics "cast" methods, will ignore ANY objects touching them when they start.
You are doing something wrong. Layermask is not the final argument in EITHER of the $$anonymous$$ethod's contructors.
Hello. From the Unity Script Reference for SphereCastNonAlloc.
public static int SphereCastNonAlloc(Vector3 origin, float radius, Vector3 direction, RaycastHit[] results, float maxDistance = $$anonymous$$athf.Infinity, int layer$$anonymous$$ask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
QuerryTriggerInteraction would be the last argument of course, but since it already has a default value assigned to it, omitting that argument just uses the default value.
But just for the sake of getting to the bottom of this, I did try setting it in the method manually and got the same results.
Ok, sorry it was of no help :) Thanks for following it up. If all normals are inverse then you are halfway there, just convert them. If its giving 0,0,0 all the time is this perhaps local and you need to add a world/local conversion; 'TranformPoint'/'InverseTransform Point' etc
How do you iterate through the results? Do you only read "count" elements from the array?
Answer by GAMEDIA_Justin · Mar 15, 2019 at 12:05 PM
Physics.SphereCastNonAlloc doesn't have this note in the documentation, but the documentation for Physics.SphereCastAll has the following note:
Notes: For colliders that overlap the sphere at the start of the sweep, RaycastHit.normal is set opposite to the direction of the sweep, RaycastHit.distance is set to zero, and the zero vector gets returned in RaycastHit.point. You might want to check whether this is the case in your particular query and perform additional queries to refine the result. Passing a zero radius results in undefined output and doesn't always behave the same as Physics.Raycast.
We were only interested in the point and it didn't have to be that precise. So we checked for every returned hit if the distance was 0f, and used hit.collider.bounds.center instead of hit.point.
Your answer
Follow this Question
Related Questions
How can the resultant velocity of an object after a potential collision be predicted? 1 Answer
SphereCast Arguments 1 Answer
How to get the Distance, Collision and Direction for each game object in my list? 2 Answers
Which is more efficent? SphereCast, SphereCastAll, or OverlapSphere? 1 Answer
Spherecast preview? 0 Answers