- Home /
OnCollisionEnter sensitivity
I'm creating a casual basketball game and I'm having a hard time configuring the colliders sensitivity.
More specifically: I put a mesh collider on the rim to detect if a swish (only net basket) is scored, but even when the ball seems to pass right through without touching anything, OnCollisionEnter triggers on the rim.
For example, in the screenshot, when the ball falls vertically the collider triggers. Is there a way to adjust the sensitivity so that it doesn't?
Answer by oscarAbraham · Nov 03, 2021 at 08:52 PM
There's no concept of sensitivity in this matter. Either an object entered a trigger or it didn't. It'd be easier to help you if you provided some images with the actual shape of mesh collider and its configuration in the inspector.
That said, I bet this is the root of your problem: I think you're using your mesh collider with Convex and Trigger turned on. Rings are concave shapes (i.e. non-convex). To make a mesh collider work as a trigger, you must have turned on the "convex" option. Convex meshes are those where a line can be drawn between every pair of vertices without going outside the mesh; so a mesh with a hole like yours doesn't fit the category, and its hole will be ignored.
Here are two possible solutions:
1. Use multiple box colliders make up the shape of the ring. You'll likely receive multiple trigger callbacks, one for each collider that was triggered, so your code will need to be able to ignore the extra calls.
2. Use a cylinder trigger that covers the complete basket hole. Then, in your trigger callback, measure the distance from the ball to the center of the cylinder: if the distance is small enough, the mesh wasn't touched. I'd prefer this solution because it's cleaner and allows you to easily add other functionality. For example, now you can change the distance-to-center threshold according to difficulty settings. You can also use the same collider to detect all kinds of stuff, like the speed of the ball, or if the ring is being touched.
Answer by Yiorgos · Nov 03, 2021 at 10:33 PM
Thanks for the detailed answer, Oscar.
Sadly, I've already tried adding box colliders around the rim and I had the same issue. By the way, I'm using OnCollisionEnter, not OnTriggerEnter, so my rim mesh collider isn't marked as convex/trigger. Moreover, when the ball falls directly in the middle, OnCollisionEnter is not triggered so the hole is not totally ignored.
Hm. It feels like you'd have a lot more control with a trigger instead of a solid collider. Anyway, then all you have to do is adjust the shape of the net collider to make it easier for the ball to pass through. This guide teaches you how to preview collider shapes to make things easier: https://docs.unity3d.com/Manual/PhysicsDebugVisualization.html
You shouldn't post this as an answer, though; this should be a comment under my answer.
You are right about the comment, my bad.
Thanks for the tip regarding the Physics Debug Visualization, I wasn't aware of it. Using it, I noticed my rigidbody was extruding from the ball gameObject when the ball was moving fast (I added a screenshot in the original question). After some trial and error, I concluded it's the Collision Detection option of the rigidbody that does the trick. I was using Continuous Dynamic in order to keep my ball from passing through the glass when shot at high speed, and this seems to have caused the issue I was having. Changing Collision Detection of the ball to Discreet, makes the rim collision work almost perfect. Thanks, you've been a great help :)
I'd advise against disabling Continuous Dynamic for fast moving objects. It does make fast moving objects less prone to collide, but not the way you want. This will make your ball skip colliders when they are thin enough or when it's moving fast enough.
Your answer
Follow this Question
Related Questions
Strange issue with collider 1 Answer
Why when i move the player object through the door the ontriggerenter/exit event are not fire ? 2 Answers
Reset a collider during runtime to lose previous Physics.IgnoreCollision() calls 1 Answer
How can I change animation with colliders? Using AR and Vuforia. 0 Answers
How to detect collision between 2 objects while checking are they the ones that need to collide? 1 Answer