- Home /
Question by
pastersteli · Oct 06, 2021 at 02:35 PM ·
physicsdots
Collide or triger not happening unity dots
Physics body works, object are moving but any collide or triger event not happening.
using System.Collections.Generic;
using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using BurstCompile = Unity.Burst.BurstCompileAttribute;
public class Collidesystem : SystemBase
{
BuildPhysicsWorld _buildPhysicsWorld;
StepPhysicsWorld _stepPhysicsWorld;
EndSimulationEntityCommandBufferSystem _commandBufferSystem;
protected override void OnCreate()
{
_buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
_stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
_commandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var collideData = GetComponentDataFromEntity<CollideData>(isReadOnly: true);
var controlData = GetComponentDataFromEntity<ControlData>(isReadOnly: true);
var destroyJob = new CollideJob
{
collideGroup = collideData,
controlGroup = controlData,
commandBuffer = _commandBufferSystem.CreateCommandBuffer()
};
this.Dependency = destroyJob.Schedule(_stepPhysicsWorld.Simulation, ref _buildPhysicsWorld.PhysicsWorld, this.Dependency);
var triggerJob = new TriggerJob
{
collideGroup = collideData,
controlGroup = controlData,
commandBuffer = _commandBufferSystem.CreateCommandBuffer()
};
this.Dependency = triggerJob.Schedule(_stepPhysicsWorld.Simulation, ref _buildPhysicsWorld.PhysicsWorld, this.Dependency);
_commandBufferSystem.AddJobHandleForProducer(this.Dependency);
}
}
[BurstCompile]
struct CollideJob : ICollisionEventsJob
{
[ReadOnly] public ComponentDataFromEntity<CollideData> collideGroup;
[ReadOnly] public ComponentDataFromEntity<ControlData> controlGroup;
public EntityCommandBuffer commandBuffer;
public void Execute(CollisionEvent ctx)
{
Entity A = ctx.EntityA, B = ctx.EntityB;
if (collideGroup.HasComponent(A))
{
if (controlGroup.HasComponent(B))
{
JumpData data = new JumpData();
data.Speed = new Vector3(0, -1, -1);
commandBuffer.AddComponent(B, data);
Debug.Log("collide");
}
}
}
}
[BurstCompile]
struct TriggerJob : ITriggerEventsJob
{
[ReadOnly] public ComponentDataFromEntity<CollideData> collideGroup;
[ReadOnly] public ComponentDataFromEntity<ControlData> controlGroup;
public EntityCommandBuffer commandBuffer;
public void Execute(TriggerEvent ctx)
{
Entity A = ctx.EntityA, B = ctx.EntityB;
if (collideGroup.HasComponent(A))
{
if (controlGroup.HasComponent(B))
{
JumpData data = new JumpData();
data.Speed = new Vector3(0, -1, -1);
commandBuffer.AddComponent(B, data);
Debug.Log("trigger");
}
}
}
}
Comment