Hey. I am new to ECS, specially the latest package versions, and I am facing difficulties getting even a simple collision detection system to fire properly. All I want to do is detect a collision with a console message and even that seems to be beyond me. The code below compiles fine and it’s an adaption of some older examples I found, using the “new” SystemBase instead of the JobComponentSystem. However, when I shoot my projectiles all over the scene and see them hit (and bounce of) my targets, no console message shows up. At this point I am little frustrated with this, so please any help or comment you could provide, would be most welcomed. Thanks in advance.
using UnityEngine;
using Unity.Jobs;
using Unity.Entities;
using Assets.ACS.Scripts.DataComponents;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Burst;
namespace Assets.ACS.Scripts.Systems
{
[UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
[UpdateAfter(typeof(ExportPhysicsWorld))]
[UpdateBefore(typeof(EndFramePhysicsSystem))]
public partial class ACS_CollisionSystem : SystemBase
{
BeginInitializationEntityCommandBufferSystem ecbSystem;
BuildPhysicsWorld buildPhysicsWorldSystem;
StepPhysicsWorld stepPhysicsWorldSystem;
protected override void OnCreate()
{
ecbSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
buildPhysicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<BuildPhysicsWorld>();
stepPhysicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<StepPhysicsWorld>();
RequireForUpdate(GetEntityQuery(new EntityQueryDesc
{
All = new ComponentType[] { typeof(ACS_ProjectileData) }
}));
}
[BurstCompile]
private struct CollisionEventJob : ICollisionEventsJob
{
public void Execute(CollisionEvent collisionEvent)
{
Debug.Log("Collision");
}
}
protected override void OnStartRunning()
{
base.OnStartRunning();
this.RegisterPhysicsRuntimeSystemReadOnly();
}
protected override void OnUpdate()
{
// Schedule physics check
Dependency = new CollisionEventJob().Schedule(stepPhysicsWorldSystem.Simulation, Dependency);
}
}
}