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

Collision Detection

Discussion in 'Entity Component System' started by Zeraphan, Mar 4, 2020.

  1. Zeraphan

    Zeraphan

    Joined:
    Nov 20, 2019
    Posts:
    14
    I am trying to create a collision system that lets me know when anything enters the firing range of my ships. I have added spherical colliders to the ships and I am following the Physics examples from Unity.

    I have this for what is getting added to the ship entities:
    Code (CSharp):
    1.  
    2.         var material = new Unity.Physics.Material
    3.         {
    4.             CustomTags = Unity.Physics.Material.Default.CustomTags,
    5.             Flags = Unity.Physics.Material.MaterialFlags.EnableCollisionEvents |
    6.                     Unity.Physics.Material.MaterialFlags.EnableMassFactors |
    7.                     Unity.Physics.Material.MaterialFlags.EnableSurfaceVelocity,
    8.             Friction = Unity.Physics.Material.Default.Friction,
    9.             FrictionCombinePolicy = Unity.Physics.Material.Default.FrictionCombinePolicy,
    10.             Restitution = Unity.Physics.Material.Default.Restitution,
    11.             RestitutionCombinePolicy = Unity.Physics.Material.Default.RestitutionCombinePolicy,
    12.         };
    13.        
    14.         var boxCollider = Unity.Physics.SphereCollider.Create(
    15.             new SphereGeometry
    16.             {
    17.                 Center = new float3(0f,0f,0f),
    18.                 Radius = 5f
    19.             },
    20.             CollisionFilter.Default,
    21.             material
    22.         );
    For the system I have the following:
    Code (CSharp):
    1.  
    2. [UpdateAfter(typeof(EndFramePhysicsSystem))]
    3. public class TargetingSystem : JobComponentSystem
    4. {
    5.     BuildPhysicsWorld m_BuildPhysicsWorldSystem;
    6.     StepPhysicsWorld m_StepPhysicsWorldSystem;
    7.     private EntityQuery shipGroup;
    8.     private EntityQuery baseGroup;
    9.  
    10.     protected override void OnCreate()
    11.     {
    12.         m_BuildPhysicsWorldSystem = World.GetOrCreateSystem<BuildPhysicsWorld>();
    13.         m_StepPhysicsWorldSystem = World.GetOrCreateSystem<StepPhysicsWorld>();
    14.      
    15.         shipGroup = GetEntityQuery(typeof(Ship),typeof(Translation));
    16.         baseGroup = GetEntityQuery(typeof(Base),typeof(Translation));
    17.    
    18.     }
    19.    
    20.     //[BurstCompile]
    21.     public struct TargetingJob : ICollisionEventsJob
    22.     {
    23.         public float deltaTime;
    24.         [ReadOnly]public ComponentDataFromEntity<Ship> ships;
    25.         [ReadOnly]public ComponentDataFromEntity<Base> bases;
    26.      
    27.         public void Execute(CollisionEvent collisionEvent)
    28.         {
    29.             Entity entityA = collisionEvent.Entities.EntityA;
    30.             Entity entityB = collisionEvent.Entities.EntityB;
    31.          
    32.             Debug.Log("collisions");
    33.         }
    34.      
    35.     }
    36.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    37.     {
    38.         var job = new TargetingJob
    39.         {
    40.             deltaTime = Time.DeltaTime,
    41.             ships = GetComponentDataFromEntity<Ship>(),
    42.             bases = GetComponentDataFromEntity<Base>(),
    43.         }.Schedule(m_StepPhysicsWorldSystem.Simulation,
    44.             ref m_BuildPhysicsWorldSystem.PhysicsWorld, inputDeps);
    45.              
    46.         return job;
    47.     }
    48. }
    The colliders work with raycasting so I can click on the ships and get a response that way. I can't get anything to happen within this system though.
     
  2. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    34
    Same for me @Zeraphan . Have you got a chance to find a solution?
     
  3. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    You need to add a Physics Shape and Physics Body authoring and check "Raise Collision Events" in the Physics Body inspector. I'm not sure if you can do this from code, but if you can't, try creating a prefab with the authoring in place and flag checked, and instantiate that in your system