Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How to react to Collision Events?

Discussion in 'Physics Previews' started by Vexille, Apr 26, 2019.

  1. Vexille

    Vexille

    Joined:
    Nov 4, 2015
    Posts:
    5
    I've read through the manual page for Unity Physics and while it has many examples on spatial queries and filtering, it doesn't say much about how to react to a collision that happened between two rigid bodies (or a trigger, for that matter). In the Core Components page, it talks about the EnableCollisionEvents flag in the collider material, but doesn't explain how to use it.

    I found the Physics Debug Display, which can draw the collision events in the scene view, so I dug around on how it does it. I've managed to hack together a way to get to the collision events, but in a very roundabout way that shouldn't be the correct way to do it:

    Code (CSharp):
    1. [UpdateAfter(typeof(StepPhysicsWorld)), UpdateBefore(typeof(EndFramePhysicsSystem))]
    2.     public class ProjectileDamageSystem : ComponentSystem
    3.     {
    4.         private StepPhysicsWorld m_stepPhysicsWorldSystem;
    5.         private BuildPhysicsWorld m_buildPhysicsWorldSystem;
    6.  
    7.         private EntityQuery m_projectiles;
    8.  
    9.         protected override void OnCreate()
    10.         {
    11.             m_stepPhysicsWorldSystem = World.GetOrCreateSystem<StepPhysicsWorld>();
    12.             m_buildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
    13.  
    14.             m_projectiles = GetEntityQuery(typeof(Projectile));
    15.         }
    16.  
    17.         protected override void OnUpdate()
    18.         {
    19.             PhysicsWorld physicsWorld = m_buildPhysicsWorldSystem.PhysicsWorld;
    20.             CollisionEvents collisionEvents = m_stepPhysicsWorldSystem.Simulation.CollisionEvents;
    21.  
    22.             Debug.Log("Updating!");
    23.  
    24.             foreach (CollisionEvent collisionEvent in collisionEvents)
    25.             {
    26.                 Entity entityA = physicsWorld.Bodies[collisionEvent.BodyIndices.BodyAIndex].Entity;
    27.  
    28.                 if (EntityManager.HasComponent<Projectile>(entityA))
    29.                 {
    30.                     Projectile projectile = EntityManager.GetComponentData<Projectile>(entityA);
    31.                     projectile.TimeToLive = 0f;
    32.                     PostUpdateCommands.SetComponent(entityA, projectile);
    33.                 }
    34.             }
    35.         }
    36.     }
    So, plugging myself between the StepPhysicsWorld and the EndFramePhysicsSystem systems, having an arbitrary EntityQuery just so the system runs and iterating over the collision events from the StePhysicsWorldSystem's Simulation.

    And an interesting note, that Debug.Log is there simply because without it, the system wouldn't consistently update o_O So this really doesn't seem like the proper way of checking for collisions. Can anybody help?
     
  2. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
  4. X0RSH1FT

    X0RSH1FT

    Joined:
    Feb 1, 2013
    Posts:
    9
    I would very much like to know how to do this as well. I've got a thread sitting in the main DOTS forum with no luck either. (https://forum.unity.com/threads/physics-callback-sample.689209/)

    It looks like the CollisionEvents accessor is no longer accessible in the most recent version of the physics package. I've been trying to find an example of someone using the simulation callback delegates mentioned here. (https://docs.unity3d.com/Packages/com.unity.physics@0.0/manual/simulation_modification.html)
     
  5. X0RSH1FT

    X0RSH1FT

    Joined:
    Feb 1, 2013
    Posts:
    9
  6. stefan-falk

    stefan-falk

    Joined:
    Nov 24, 2019
    Posts:
    15