Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question OnCollisionEnter gets called, but collision won't affect rigidbody.

Discussion in 'Physics' started by Bartalo, May 19, 2023.

  1. Bartalo

    Bartalo

    Joined:
    Aug 5, 2013
    Posts:
    2
    Hello people,

    After looking online I cannot find any suggestion for an issue I am experiencing (in Unity 2017):

    I am doing a space sim combat game where fast objects have to collide together.

    Sometimes, when a collision happens (or ought to happen anyway), whatever code i do write in the 'onCollisionEnter' call gets executed properly, but the rigidbody does not get affected in any way by the collision, passing through the collider with no change in velocity.

    This seems to happen kinda randomly, but way more often in certain situations. By debugging the onCollisionEnter call, i see that these missed collisions seem to happen at speeds of around 900-1000 units (collision.relativeVelocity.magnitude) and by small 'missile' rigidbody hitting large spaceships (collider is around x20 times larger), whilst the masses are of exactly 3000 and 13620000 units.

    I tried reducing the larger mass by a factor of 100 but the problem someway gets worse, with the smaller rigidbody getting stuck midway inside the larger, heavier one.

    What's even stranger is that faster speeds with the same colliders work better and almost never fail.

    I have a suspicion where the issue might lie: in my game, before every fixedUpdate, the rigidbody position and velocity of all rigidbodies in the scene get set by retrieving doubles, while velocity gets changed slightly; and everything gets centered at the player's position and velocity by subtracting doubles.

    After every fixed Update, these doubles get updated with the difference of the rigidbody state that just got set, and of the retrieved state of the rigidbody after physical simulation. So the rigidbodies get placed manually in the scene before and after the fixedUpdate. Transforms get placed too in exactly the same values of the rigidbodies.

    I also mess with the 'isKinematic' flag but i don't think this is the problem.

    But i think it may be slight errors in these calculations to be causing these issues. I would investigate this suspicion more carefully before opening this thread, if it weren't that 1) the system i have is absolutely required by the game, 2) messing with it would need much time and effort and break the game totally, 3) the system seems to run almost perfectly BUT for certain situations.

    I regularly smash huge spaceships together at incredible speeds and need no fixes to unity's physics but for some easy corrections to torque (which seems to get ignored above certain speeds).

    So before messing with things too much, i ask whether you guys have any idea what i might look into in order to solve the issue. The salient point seems to be that onCollisionEnter gets called properly, so some collision does happen and does get registered, but the rigidbody won't be affected by the collision.

    Thanks very much for your attention.
     
  2. miguel0l

    miguel0l

    Joined:
    Nov 10, 2021
    Posts:
    13
    It might be an issue with how your rigidbody is detecting collisions when it comes to super fast objects. You can try tweaking the rigidbody settings, namely Interpolate and Collision Detection. You can try setting the Interpolate setting to Interpolate, cause I think its set to None by default.

    If it helps, heres an article (as well as a few others linked inside it) that might help you figure out rigidbodies better!

    https://blog.terresquall.com/2023/09/what-is-a-kinematic-rigid-body-and-how-are-they-used-in-unity/