Why does a SphereCast require a direction, shouldn't a sphere be in all directions? and how to cast a sphere around my object from it's center in all directions
I'm trying to capture Raycast hits; to get the normal of the objects around my player as in the picture. I've been using Physics.spherecast but its behaving as if it's a Raycast, that is it's capturing Hits in the given direction.
Answer by WillNode · Sep 21, 2016 at 01:54 PM
I'm sure that this is what you're looking for : Physics.OverlapSphere
SphereCast is like a ray, but with thickness along the ray with given radius (like a capsule).
EDIT: SphereCast still does what you wanted. It's parameter may seems confusing, but you can put the direction into Vector3.zero to get a perfect spherical shape. Don't this is what you meant?
Overlap sphere is working fine, with the exception of one thing. it doesn't get me a RaycastHit which I need to find the surface normal, it returns a collider ins$$anonymous$$d, any ideas how to get the normal or a Raycasthit from a collider?
Okay, updated my answer. If you need those RaycastHit
shouldn't a sphere be in all directions?
Yes. But spherecast moves a sphere through the world in certain direction and certain distance and sees what it hits on its way.
put the direction into Vector3.zero
or at least the distance as 0.
This should not be the accepted answer.
OverlapSphere is great when you just need to know what you hit but no hit info (i.e. normal, hit point, distance, etc.). This is not what the OP needed.
SphereCast only returns a hit info after it has moved. A SphereCast with a direction of Vector3.zero will not return a hit even if it should.
I recommend looking at advo's answer for more detail.
Answer by advo · Apr 28, 2020 at 05:14 PM
Guys i ran into similar problems and here is what I found.
Setting "direction" as Vector3.zero just caused the Spherecast to not work regardless of "maxDistance".
Setting "direction" as the forward direction (e.g. transform.forward), and setting the maxDistance of a small value example 0.1f, worked.
In my testing,the Spherecast only picks up collisions that happen at the "front half" surface of the Sphere, front being the "direction" that the "Sphere" being casted. Hence if you were to have an enemy facing the positive Z direction and constantly casting a SphereCast of maxDistance of 0.1f (or any maxDistance) in the forward direction, you will notice that a hit is only registered when a collider intersects with the front half surface of the Sphere.
If you move a collider into the sphere from the rear half, it does not register the collision. It does not detect collisions in the rear half of the Sphere, or anything that is already "inside" the Sphere.
Think of the the Spherecast being more of a U-shaped "Sonic Boom" shape cast by Guile from Street Fighter.
Which also means if your "origin" parameter of the Spherecast call, fell right smack on a small target that you intended the cast to collide with, and if your sphere radius is larger than the target collider, you might not actually register a hit...
public static bool SphereCast(Vector3 origin, float radius, Vector3 direction, out RaycastHit hitInfo, float maxDistance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
Answer by Bonfire-Boy · Sep 21, 2016 at 01:54 PM
That's what sphere cast does. From the documentation, "Think of the sphere cast like a thick raycast."
https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
You might want to look at OverlapSphere as an alternative
https://docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html
Overlap sphere is working fine, with the exception of one thing. it doesn't get me a RaycastHit which I need to find the surface normal, it returns a collider ins$$anonymous$$d, any ideas how to get the normal from a collider?
Hmm. so what you actually want is a RayCast that sends rays in every direction. That would be quite expensive. Someone else may have a better idea but the first thing that springs to my $$anonymous$$d is to use OverlapSphere to get a list of candidates and then RayCast at each of them individually to get a contact point etc (or SphereCast, with the size deter$$anonymous$$ed by the size of the candidate).
Your answer
Follow this Question
Related Questions
SphereCastAll hit.point are allways zero 1 Answer
The SphereCastAll is not picking up certain objects 1 Answer
AI and Enemy Detection 1 Answer
Having Troubles with Sphere Casts 0 Answers