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

Update component data on collision

Discussion in 'Physics Previews' started by stefan-falk, Jan 5, 2020.

  1. stefan-falk

    stefan-falk

    Joined:
    Nov 24, 2019
    Posts:
    15
    So, I am currently trying to get collisions work in my project.

    At the moment I am not even sure if the code that would handle the collision gets executed at all.

    All it is supposed to do is set the speed value of a collided entity to 100 but running the code does not do anything.

    The simulation contains a few hundred capsules which collide with each other. Everything is working but the collisions do not have the desired effect: Speed up one of the collided entities.

    Here is the code I am currently using:

    Code (CSharp):
    1. using ECS.Components;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using Unity.Physics;
    5. using Unity.Physics.Systems;
    6.  
    7. namespace ECS.Systems
    8. {
    9.     [UpdateAfter(typeof(EndFramePhysicsSystem))]
    10.     public class CollisionEventSystem : JobComponentSystem
    11.     {
    12.         private BuildPhysicsWorld _physicsWorld;
    13.  
    14.         private StepPhysicsWorld _stepPhysicsWorld;
    15.  
    16.         protected override void OnCreate()
    17.         {
    18.             this._physicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
    19.             this._stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
    20.         }
    21.  
    22.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    23.         {
    24.             var handle = new CollisionEventSystemJob
    25.                 {
    26.                     SpeedComponentData = GetComponentDataFromEntity<Speed>()
    27.                 }
    28.                 .Schedule(this._stepPhysicsWorld.Simulation, ref this._physicsWorld.PhysicsWorld, inputDeps);
    29.             handle.Complete();
    30.             return handle;
    31.         }
    32.     }
    33.    
    34.     struct CollisionEventSystemJob : ICollisionEventsJob
    35.     {
    36.         public ComponentDataFromEntity<Speed> SpeedComponentData;
    37.  
    38.         public void Execute(CollisionEvent collisionEvent)
    39.         {
    40.             var entityA = collisionEvent.Entities.EntityA;
    41.             // var entityB = collisionEvent.Entities.EntityB;
    42.            
    43.             if (SpeedComponentData.Exists(entityA)  )
    44.             {
    45.                 Speed speed = SpeedComponentData[entityA];
    46.                 speed.Value = 100;
    47.             }
    48.         }
    49.     }
    50. }
    Am I missing something here?
     
  2. hardcodednumber

    hardcodednumber

    Joined:
    May 26, 2014
    Posts:
    88
    you need to set the component 'Speed' back to the component data array. i.e:
    Code (CSharp):
    1.  
    2. ...      
    3. var entityA = collisionEvent.Entities.EntityA;
    4. // var entityB = collisionEvent.Entities.EntityB;
    5.          
    6.     if (SpeedComponentData.Exists(entityA)  )  {
    7.                 Speed speed = SpeedComponentData[entityA];
    8.                 speed.Value = 100;
    9.               SpeedComponentData[entityA] = speed;
    10.       }
    11. ....
    12.  
     
    edalbeci likes this.
  3. stefan-falk

    stefan-falk

    Joined:
    Nov 24, 2019
    Posts:
    15
    Oh, that was indeed it. Why is that I wonder? I expected
    speed
    to be a reference to the actual struct.

    In any case: Thank you very much! :)
     
  4. edalbeci

    edalbeci

    Joined:
    Jan 21, 2018
    Posts:
    36