Update component data on collision

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:

using ECS.Components;
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;

namespace ECS.Systems
{
    [UpdateAfter(typeof(EndFramePhysicsSystem))]
    public class CollisionEventSystem : JobComponentSystem
    {
        private BuildPhysicsWorld _physicsWorld;

        private StepPhysicsWorld _stepPhysicsWorld;

        protected override void OnCreate()
        {
            this._physicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
            this._stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
        }

        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var handle = new CollisionEventSystemJob
                {
                    SpeedComponentData = GetComponentDataFromEntity<Speed>()
                }
                .Schedule(this._stepPhysicsWorld.Simulation, ref this._physicsWorld.PhysicsWorld, inputDeps);
            handle.Complete();
            return handle;
        }
    }
   
    struct CollisionEventSystemJob : ICollisionEventsJob
    {
        public ComponentDataFromEntity<Speed> SpeedComponentData;

        public void Execute(CollisionEvent collisionEvent)
        {
            var entityA = collisionEvent.Entities.EntityA;
            // var entityB = collisionEvent.Entities.EntityB;
           
            if (SpeedComponentData.Exists(entityA)  )
            {
                Speed speed = SpeedComponentData[entityA];
                speed.Value = 100;
            }
        }
    }
}

Am I missing something here?

you need to set the component ‘Speed’ back to the component data array. i.e:

...      
var entityA = collisionEvent.Entities.EntityA;
// var entityB = collisionEvent.Entities.EntityB;
          
    if (SpeedComponentData.Exists(entityA)  )  {
                Speed speed = SpeedComponentData[entityA];
                speed.Value = 100;
              SpeedComponentData[entityA] = speed;
      }
....
1 Like

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! :slight_smile:

I just responded in your other thread but duplicating here for others who find this thread:

I I think the write fails because “by default, you cannot write to components in the container in parallel Jobs” https://docs.unity3d.com/Packages/c…Unity.Entities.ComponentDataFromEntity-1.html.