Search Unity

Collision detection firing too late or too early

Discussion in 'Physics' started by fleacoco, Jun 3, 2019.

  1. fleacoco

    fleacoco

    Joined:
    Mar 6, 2019
    Posts:
    5
    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://imgur.com/w9KMEff. 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;
    }
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
  3. fleacoco

    fleacoco

    Joined:
    Mar 6, 2019
    Posts:
    5
    Thanks I will try. So I guess I should run OverlapShere inside FixedUpdate ?
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Depends. If you move your grenade by the physics - then yes.
    Otherwise check at the code where you're performing your movement.