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?