#dots Trigger Event problem

1 messages · Page 1 of 1 (latest)

terse kindle
#

hi guys.

i have a prefab. it works fine when physics shape's collision response set collide.
but i can't trigger my ITriggerEventsJob.

when i change the collision response to "Raise Trigger Events", it works fine for
ITriggerEventsJob . but it will falling from the floor.

How should I handle this situation in unity dots?

boreal hinge
#

Are you looking for ICollisionEventsJob?

coarse sundial
#

trigger就不用加physic body啦 或者设置成kinematic

terse kindle
#

我现在的技术方式是打算实现一个多选框, 通过制造一个多选的碰撞体去碰撞区域内所有的可选择单元去进行多选。
这就意味着这里的被选择对象需要两个特性, 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?

boreal hinge
#

CollideRaiseCollisionEvents sends events to ICollisionEventsJob, not ITriggerEventsJob

terse kindle
#

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);
    }
}
broken parcel
#

Collision events only appear if physics resolves them via physics velocity

#

If you just put colliders together

#

That won't trigger anything

terse kindle
#

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

#

https://github.com/JohnnyTurbo/2020-LTS-ECS-Tutorials/blob/a76c44166d45c36497bd6b288c62ba8d9d36e1bb/ECSTutorials-URP-Project/Assets/ECS_UnitSelection/Scripts/Systems/UnitSelectionSystem.cs#L32

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.

GitHub

Container for project files for ECS tutorials I create using 2020.3 (LTS) for the Turbo Makes Games YouTube channel. - JohnnyTurbo/2020-LTS-ECS-Tutorials

terse kindle
#

overlap是啥 有例子吗?

coarse sundial
terse kindle
#

感谢 我学一下

coarse sundial
#

unity github has this example

terse kindle
#

thanks

broken parcel
#

yeah, no need to create anything

#

just need to use method on physics world

terse kindle
#

OverlapSphere doesn't work well for me。 it only overlap sphere, but in my scene, i need a OverlapRectangle function

broken parcel
#

Overlap box will do

terse kindle
#

Overlap box seems that it can only handle cubes, not rectangles.

broken parcel
terse kindle
#

thanks. i got it.

empty grotto
#

i am not able to understand why Trigger gets fired multiple times

boreal hinge
#

Sounds like it should be its own thread as is its a different issue?

empty grotto
#

yea

#

my bad

#

was going to post it in general

#

posted it in the wrong thread