- Home /
Collider suddenly stops detecting collision? (bug?)
So a few months back I was working in a 2D project. It happened very frequently where 2D colliders (box etc) suddenly stop detecting collision for no reason. I have to disable/re-enable the collider or the gameObject the collider's attached to for collision detection to get back. I thought this was a 2D bug. After some searching, I found a lot of people complaining about 2D so yeah, it's a 2D bug... I got around it by writing a script that 'refreshes' the object on certain intervals (very silly)
But now I'm in 3D, and actually just came across this problem... I was recording a demo for my team and it happened a couple of times during that video. Have a look (@1:55 where I was shooting the left arm, but it didn't react till I disabled/renabled its collider. Also its clearly visible after 3:10 - The right legs/thighs and his left arm afterwards... the raycast is just going right through them to the other side...)
If anyone have any idea It'd be really appreciated.
Thanks!
Edit:
A little info: I'm not instantiating bullets with speed and detecting their collision with body parts, I'm just using raycasts to shoot. Notice at 3:10 the raycast isn't even intersecting/hitting the legs - when I disable/renable the leg it immediately detects the ray... In other words I don't really need to shoot for some parts not to detect the raycast...
I actually have two raycasts, one for debugging (why you see the red ray with the red dot at the end...), and one for shooting (does the actual detection)
private void OnDrawGizmos()
{
Vector3 tipPos = gunTip.position;
Vector3 tipFwd = gunTip.forward;
Vector3 end;
RaycastHit hit;
if (Physics.Raycast(tipPos, tipFwd, out hit, Distance, shootingMask))
{
end = hit.point;
}
else end = tipPos + tipFwd * Distance;
GizHelper.DrawLine(tipPos, end, Color.red);
GizHelper.DrawSphere(dbgLastShot, .025f, Color.red);
GizHelper.DrawSphere(end, .025f, Color.red);
}
private void Shoot()
{
// Sound, muzzle and ammo decrease...
RaycastHit hit;
if (Physics.Raycast(gunTip.position, gunTip.forward, out hit, Distance, shootingMask))
{
log("Shot: " + hit.collider.name);
dbgLastShot = hit.point;
var receiver = hit.collider.GetInterface<IHitReceiver>();
if (receiver != null)
{
receiver.ReceiveHit(gunTip.position, damage);
}
}
}
$$anonymous$$ay be bullet moves too fast and passes collider between two physics updates. Did you try to decrease fixed timestep in Project Settings->Time? You can lower bullet speed to check if this is the reason.
Static colliders. Add a kinematic rigidbody to each collider object so the collision system maintains its state correctly.
Old answer --
Unity Physics is not designed to work out of the box for small fast moving objects, or even large fast moving objects for that matter. Your issue is almost certainly due to the bullet going completely through the collider between physics updates (as mentioned by @JustFun). $$anonymous$$ost of the "misses" are when you are ai$$anonymous$$g at the edge of the collider where the depth approaches zero, making it much more likely to miss the closer you aim to the edge. You can calculate the distance an object travels between physics updates by "velocity * Time.fixedDeltaTime"... if this is larger than your objects diameter you can go through walls, and even if much smaller you can still have misses towards collider corners/edges.
What you need is swept collisions for which you'll need to roll your own system. This is what I might do:
Store bullet position "previousPosition" at the end of each FixedUpdate (and initially on bullet instantiation)
Call a Physics.SphereCast method with RaycastHit info using the previous position, current position and a small radius. Use collision layers if needed to specify what can be 'hit'.
Use the RaycastHit info to deter$$anonymous$$e the collision response you should perform. This gives you the collision position, normal etc and you can combine this with the bullets velocity and mass to add a Physics based response to the hit body part using AddForce, AddTorque etc. Or you can do something simpler of course.
You can also perform this process in Update rather than FixedUpdate as it doesn't matter how many incremental moves have occurred between frames if you sweep the bullets path since you last checked.
Hi there thanks a lot for your answer. However;
Your issue is almost certainly due to the bullet going completely through the collider between physics updates
I'm sorry I should have mentioned this, but I'm not instantiating bullets with speed or anything, I'm just using raycasts to shoot (added detail to question). Notice at 3:10 the raycast isn't even intersecting/hitting the legs - when I disable/renable the leg it immediately detects the ray... In other words I don't really need to shoot for some parts not to detect the raycast...
Hmmm... well that is bizarre. Are you using any special collision layers? $$anonymous$$ake sure your colliders aren't set to ignore raycasts. Of course this wouldn't explain why toggling the game object fixes the issue.
I'm using a shooting$$anonymous$$ask
but it's set to "Everything" so nothing is ignored... Yes that's pretty much the most bizzare thing, what happens/why does it work when I toggle the object..? (or just the collider for that matter)...
Hi @JustFun thanks for your input, please see my edit. I'm not instantiating bullets I'm using raycasts.
Nearly two years later and this problem is still in effect with Unity 5.3.5. Huacanacha says it best. Currently working on a 2D project and for some reason this is happening. Added Rigidbody2D with kinematic and problem went away.
I've been experiencing this as well, but with 3D Character controllers falling through box colliders. Anyone?
Answer by Huacanacha · Aug 17, 2014 at 10:44 AM
I now suspect: static colliders.
"Worse still, the changes can sometimes leave the collider in an undefined state that produces erroneous physics calculations. For example a raycast against an altered Static Collider could fail to detect it, or detect it at a random position in space." -> from the Colliders Overview in the manual.
Try adding kinematic Rigidbodies to each object with a collider. Then when the limbs move they're not leaving the collision system in a broken state. This may also explains why disable->enable fixes the problem.
Spot on! - $$anonymous$$uch better results. So hopefully that's it. Thanks! Life saver!
i know, im still trying to find an answer, have you found one?
Your answer
![](https://koobas.hobune.stream/wayback/20220613160801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Surface with hole and Raycast - Which collider 1 Answer
Unity collision detection only for raycasts 1 Answer
Detect RaycastHit on Character Controller 2 Answers
Prevent shooting when gun is inside wall 1 Answer
raycast not colliding 1 Answer