Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by vexe · Aug 17, 2014 at 04:54 AM · collisionraycastcolliderbug-perhaps

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);
             }
         }
     }

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JustFun · Aug 17, 2014 at 07:09 AM 0
Share

$$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.

avatar image Huacanacha · Aug 17, 2014 at 09:09 AM 0
Share

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:

  1. Store bullet position "previousPosition" at the end of each FixedUpdate (and initially on bullet instantiation)

  2. 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'.

  3. 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.

avatar image vexe Huacanacha · Aug 17, 2014 at 09:37 AM 0
Share

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...

avatar image Huacanacha Huacanacha · Aug 17, 2014 at 09:45 AM 0
Share

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.

avatar image vexe Huacanacha · Aug 17, 2014 at 09:51 AM 0
Share

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)...

Show more comments
avatar image vexe · Aug 17, 2014 at 09:42 AM 0
Share

Hi @JustFun thanks for your input, please see my edit. I'm not instantiating bullets I'm using raycasts.

avatar image laszlar · Jun 02, 2016 at 03:48 AM 0
Share

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.

avatar image atmosgames · Oct 21, 2016 at 07:41 PM 0
Share

I've been experiencing this as well, but with 3D Character controllers falling through box colliders. Anyone?

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Aug 17, 2014 at 11:11 AM 0
Share

Spot on! - $$anonymous$$uch better results. So hopefully that's it. Thanks! Life saver!

avatar image $$anonymous$$ · Jul 27, 2020 at 01:25 PM 0
Share

This is still a problem in unity 2020

avatar image dkub7 $$anonymous$$ · Aug 30, 2020 at 03:19 PM 0
Share

i know, im still trying to find an answer, have you found one?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges