#dots Trigger Event problem
1 messages · Page 1 of 1 (latest)
Are you looking for ICollisionEventsJob?
trigger就不用加physic body啦 或者设置成kinematic
我现在的技术方式是打算实现一个多选框, 通过制造一个多选的碰撞体去碰撞区域内所有的可选择单元去进行多选。
这就意味着这里的被选择对象需要两个特性, 1 需要是一个物理碰撞体参与后续的逻辑, 2 需要被触发可以参与多选逻辑。 这里就很头疼了
this is my trigger code .
private void SelectMultipleUnits()
{
UnityEngine.Debug.Log("select multi units");
_isDragging = false;
....
// 创建4条射线,用于限制选择范围
var cornerRays = new[]
{
_mainCamera.ScreenPointToRay(rect.min),
_mainCamera.ScreenPointToRay(rect.max),
_mainCamera.ScreenPointToRay(new Vector2(rect.xMin, rect.yMax)),
_mainCamera.ScreenPointToRay(new Vector2(rect.xMax, rect.yMin))
};
// 创建一个数组,用于存储选择的单位
var vertices = new NativeArray<float3>(5, Allocator.Temp);
// 遍历射线,获取选择范围内的单位
for (var i = 0; i < cornerRays.Length; i++)
{
vertices[i] = cornerRays[i].GetPoint(50f);
}
// 将当前摄像机的位置添加到选择范围内
vertices[4] = _mainCamera.transform.position;
// 创建一个碰撞过滤器,用于限制选择范围
var collisionFilter = new CollisionFilter
{
BelongsTo = (uint)CollisionLayers.Selection,
CollidesWith = (uint)CollisionLayers.Units
};
// 创建一个物理材质,用于限制选择范围
var physicsMaterial = Unity.Physics.Material.Default;
physicsMaterial.CollisionResponse = CollisionResponsePolicy.CollideRaiseCollisionEvents;
// 创建一个碰撞体,用于限制选择范围
var selectionCollider = ConvexCollider.Create(vertices, ConvexHullGenerationParameters.Default,
collisionFilter, physicsMaterial);
// 创建一个新的选择实体,用于存储选择范围
var newSelectionEntity = EntityManager.CreateEntity(_selectionArchetype);
EntityManager.SetComponentData(newSelectionEntity, new PhysicsCollider { Value = selectionCollider });
}
but it can't trigger any CollisionEvent. Is there any problem here?
CollideRaiseCollisionEvents sends events to ICollisionEventsJob, not ITriggerEventsJob
i already changed it to ICollisionEventsJob.
my ICollisionEventsJob code
public struct SelectionJob : ICollisionEventsJob
{
public ComponentLookup<SelectionColliderTag> SelectionVolumes;
public ComponentLookup<Unit> Units;
public EntityCommandBuffer ECB;
public void Execute(CollisionEvent triggerEvent)
{
UnityEngine.Debug.Log("trigger event");
var entityA = triggerEvent.EntityA;
var entityB = triggerEvent.EntityB;
var isBodyASelection = SelectionVolumes.HasComponent(entityA);
var isBodyBSelection = SelectionVolumes.HasComponent(entityB);
if (isBodyASelection && isBodyBSelection)
{
return;
}
if (!isBodyASelection && !isBodyBSelection)
{
return;
}
var isBodyAUnit = Units.HasComponent(entityA);
var isBodyBUnit = Units.HasComponent(entityB);
if ((isBodyASelection && !isBodyBUnit) || (isBodyBSelection && !isBodyAUnit))
{
return;
}
var selectedUnit = isBodyASelection ? entityB : entityA;
ECB.AddComponent<SelectedUnitTag>(selectedUnit);
}
}
Collision events only appear if physics resolves them via physics velocity
If you just put colliders together
That won't trigger anything
My current technical method is to implement a multi-selection box by creating a multi-selection collision body to collide with all selectable units in the area to perform multi-selection.
This means that the selected object here needs two componnent. 1. It needs to be a physical collision body . 2. It needs to be triggered to participate in multi-selection logic. It's a headache here
I developed it according to this project,
But what I am confused about is why in this project, the object's CollisionResponse is set to Collide, but it can trigger ITriggerEventsJob.
However, ITriggerEventsJob cannot be triggered in my system.
overlap就行了
overlap是啥 有例子吗?
感谢 我学一下
unity github has this example
thanks
OverlapSphere doesn't work well for me。 it only overlap sphere, but in my scene, i need a OverlapRectangle function
Overlap box will do
Overlap box seems that it can only handle cubes, not rectangles.
You can define any size
thanks. i got it.
i am not able to understand why Trigger gets fired multiple times
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Sounds like it should be its own thread as is its a different issue?