Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

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

  1. Bartalo


    Aug 5, 2013
    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.