Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnTriggerEnter firing too late

I'm trying to make a sticky grenade by disabling its rigidbody upon collision, using OnCollisionEnter(). However, OnCollisionEnter() fires when there is clearly no collision happening, as you can see in this image : https://i.sstatic.net/8EXD3.jpg. This causes my grenade to stick in the air. I suspect this is due to a lack of synchronization between physics loop & rendering loop. Also, my standard "non-sticky" grenade uses the same model & is working finely.

I already tried the following :

  • Verify that there are no other colliders which could cause my grenade to "stick in the air"

  • Use OnTriggerEnter() instead. The result is a little better but the problem is that it is fired too late, only once the grenade is inside an obstacle or an enemy.

  • Add rigidbody & continuous collision detection to all objects

  • Tweak PhysicsManager.DefaultContactOffset from 0.01 to 0.001, as suggested by Eliasar

  • Switch from "PCM" to "Legacy Contacts Generation"

None of these worked so I'm feeling a little desperate. Thanks in advance for your help !

Code I use to stick the grenade upon collision :

void OnCollisionEnter(Collision c) {
   rigidbody.isKinematic = true;
   transform.parent = c.transform;
}
like image 995
fleacoco Avatar asked Aug 31 '25 10:08

fleacoco


1 Answers

PhysicsManager.DefaultContactOffset might be set too high for your object scale

According to this Unity forum post, your physics margin might be too high if your objects' scales are too small. OnCollisionEnter will fire during a Physics tick and may update your grenade's position a frame sooner than you're expecting. That would explain why it would stick outside the contact point, whereas OnTriggerEnter would process 1 frame later.


Unity post in text form

Max_van_Hell, Jun 1, 2017

oh god yeah Baby, I finally found the issue!

Appearantly the PhysicsManager.DefaultContactOffset was too high for my scale. The Default contact Offset was about 0.02 and with virtual objects which are around 10cm it yields a HUGE error margin obviously.

That never was a Problem for me, since in most graphics or game projects the scale of the objects is actually much larger so that such a contact Offset does not have a huge influence.

However, since this is a HoloLens Project and I am working with virtual objects which are around 10cm - 100cm the contact offset is more sensible.

Anyway, thank you much for your help!

cheers,

like image 73
Eliasar Avatar answered Sep 04 '25 04:09

Eliasar