Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Inconsistencies between physic collisions and manual collision detections using CalculateDistance ?

Discussion in 'Physics for ECS' started by Nams33, Jan 20, 2023.

  1. Nams33

    Nams33

    Joined:
    Oct 14, 2018
    Posts:
    6
    Hi,
    I encountered a case where a Unity.Physics.CollisionEvent is raised every frame, while PhysicsWorld.CalculateDistance(distanceInput, ref collector) does not return any hit.

    To give a bit more context, I am currently playing with Rival, and I move my character (capsule collider) close to a cube and encounter an edge case. The character (kinematic) stop in place when coliding with the cube (expected behavior) and I stop moving the character at all.
    At this point I have a ICollisionEventJob that triggers a collision between the character and the cube every frame. Nevertheless, inside Rival character controller, there is a a call to PhysicsWorld.CalculateDistance (with a max distance of 0) which returns only the collision with the character itself.
    Increasing the max distance to something like 0.001 actually make PhysicsWorld.CalculateDistance return a second collision.

    I would expect consistency between the two. Am I missing something ?
     
  2. philsa-unity

    philsa-unity

    Unity Technologies

    Joined:
    Aug 23, 2022
    Posts:
    115
    I can think of 2 reasons why a collision event is raised while a CalculateDistance(0f) returns nothing:
    1. If the CalculateDistance() is called after the physics update (which is the case in Rival), and the "Synchronize Collision World" option is set to true in the PhysicsStep component, then we are calculating distance after the physics was solved. So, after the other collider got decollided from the character
    2. I believe there is a certain collision tolerance value used by the physics solver to detect collision between shapes that are very close to each-other, although I'd have to double-check on that
     
    Nams33 likes this.
  3. Nams33

    Nams33

    Joined:
    Oct 14, 2018
    Posts:
    6
    I would lean toward number 2 then, as with number 1, the built in collisionEvent would not continue to trigger on every next frames.
    Good to know. Thx
     
  4. WildMaN

    WildMaN

    Joined:
    Jan 24, 2013
    Posts:
    128