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

Resolved Exit trigger event

Discussion in 'Physics for ECS' started by JediNizar, May 27, 2021.

  1. JediNizar

    JediNizar

    Joined:
    Nov 13, 2016
    Posts:
    110
    Hi I'm trying to combine the DOTS new Physic engine with the standard GameObject Physic.
    As a workaround, I attached an entity as a child to my player and make it follow the Gameobject Player, with the new Physic collider with an offset into the direction of Player facing, that it always is a bit ahead of the player
    So far so good, the Event trigger each time the "Entity palyer" collide with the target Entity
    but I need to reset the movement on Trigger exit.
    Can any1 tell me how to setup on Trigger Exit?
    here is my code
    Code (CSharp):
    1. public class TestTrigger : JobComponentSystem
    2. {
    3.     [BurstCompile]
    4.     private struct TriggerJob : ITriggerEventsJob
    5.     {
    6.         public ComponentDataFromEntity<PhysicsVelocity> physicsVelocityEntities;
    7.  
    8.         [ReadOnly] public ComponentDataFromEntity<Translation> translationData;
    9.  
    10.         public void Execute(TriggerEvent triggerEvent)
    11.         {
    12.             // EntityPair entity
    13.             SubSceneReferences.Instance.EntityCollision = false;
    14.             if (physicsVelocityEntities.HasComponent(triggerEvent.EntityA))
    15.             {
    16.                 SubSceneReferences.Instance.EntityCollision = true;
    17.                 Debug.Log("collision");
    18.             }
    19.        }
    20.     }
    21.  
    22. private BuildPhysicsWorld buildPhysicsWorld;
    23. private StepPhysicsWorld stepPhysicsWorld;
    24.  
    25. protected override void OnCreate()
    26. {
    27.         buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    28.         stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    29. }
    30.  
    31. protected override JobHandle OnUpdate(JobHandle inputDeps)
    32. {
    33.  
    34.         TriggerJob triggerJob = new TriggerJob
    35.         {
    36.             physicsVelocityEntities = GetComponentDataFromEntity<PhysicsVelocity>(),
    37.             translationData = GetComponentDataFromEntity<Translation>(true)
    38.         };
    39.         return triggerJob.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, inputDeps);
    40.   }
    41. }
    what I need is set SubSceneReferences.Instance.EntityCollision = false; on Trigger exit.
     
    Last edited: May 31, 2021
  2. papopov

    papopov

    Joined:
    Jun 29, 2020
    Posts:
    32
    There are examples of stateful events (that have Enter, Exit and Stay functionalities) in the UnityPhysicsSamples repo (EntityComponentSystemSamples/UnityPhysicsSamples/Assets/Demos/2. Setup/2d. Events/2d1. Triggers/Scripts at master · Unity-Technologies/EntityComponentSystemSamples (github.com)) The file that you're looking for is called DynamicBufferTriggerEventAuthoring.cs, and you can see how it is used in one of the samples (Assets/Demos/2. Setup/2d. Events/2d1. Triggers/2d1a. Triggers - Change Material.unity, Assets/Demos/2. Setup/2d. Events/2d1. Triggers/2d1b. Triggers - Portals.unity or Assets/Demos/2. Setup/2d. Events/2d1. Triggers/2d1c. Triggers - Force Field.unity)
     
    Steffen-ttc and JediNizar like this.
  3. JediNizar

    JediNizar

    Joined:
    Nov 13, 2016
    Posts:
    110
    Thaaaaaaaaaaaank you very much you helped me a lot
     
    papopov likes this.