- Home /
What is the proper usage of SphereCast
I am having some trouble getting Physics.SphereCast to work as I expect. My reason for using this method is to get some extra "padding" around my standard mouse raycast for touch input.
The following code snippet works just fine:
RaycastHit hit;
if (!Framework.IsMouseBlocked &&
Physics.Raycast(Framework.MouseRay, out hit, 1000, (1 << 8)))
m_hover_object = (Decoration)hit.transform.GetComponent(typeof(Decoration));
else
m_hover_object = null;
This code does not work however:
RaycastHit hit;
if (Physics.SphereCast(Framework.MouseRay.origin, 15, Framework.MouseRay.direction, out hit, 1000))
m_hover_object = hit.transform.GetComponent<Decoration>();
else
m_hover_object = null;
The purpose for using the orgin/direction override of SphereCast was just to test and see if I got a different result. My initial attempt was using the Ray override for the method, this also failed.
The selection never returns any results even when used in situations where the RayCast does.
Answer by Jessy · Jan 14, 2011 at 02:01 AM
Looks good to me, but I don't know what Framework.MouseRay.origin or .direction are. My initial thought is that your radius is too large. If the sphere is so fat that it's already colliding, and no other colliders exist in that direction, it will return false.
If that's not helpful, test without your custom data, because your methodology is sound.
There is something wrong with the way the current implementation is working. I will submit a support ticket tomorrow when I have more time.
Here is an overview of my setup and what is going wrong:
I have a game object with a box collider marked as trigger and a rigid body with gravity disabled and collision detection mode set to discrete. This object begins its life disabled and is enabled later in the scene. At this point the collision detection using SphereCast does NOT work, RayCast does.
To get the collision to work I perform the following steps in editor: 1) Uncheck the "Is Trigger" op
To get the collision to work I perform the following steps in editor: 1) Uncheck the "Is Trigger" option on the collider. 2) Switch the collision detection mode to "Continuous Dynamic" 3) Switch the collision detection mode back to "Discrete" 4) Check the "Is Trigger" option back on the collider 5) The collision using SphereCast is now working!
I just reported a bug, and then saw this. I will add this workaround to the bug report; thank you. http://answers.unity3d.com/questions/36479/physics-capsulecastall-not-detecting-kinematictrigger-colliders/36480#36480
Thanks for reporting the bug Jessy, looks like I will use a few Physics.OverlapSphere() checks ins$$anonymous$$d for the time being. I am really thankful that they are adding so many more features to the Physics system though, I look forward to future updates!
Should I mark this as the answer since it offers some form of resolution to the problem? Or should I leave it open since the problem is not "solved"? I am more of a forum guy myself, I don't know the proper answers etiquette =)