Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collision Filter

Discussion in 'Physics for ECS' started by shotoutgames, Aug 4, 2020.

  1. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    283
    How do I setup the editor / fields to have in this case my player entity not collide with itself ?
    Warning newbie even when it comes to standard physics and layermasks.
    Thanks

    Code (CSharp):
    1.  
    2.             PointDistanceInput pointDistanceInput = new PointDistanceInput
    3.             {
    4.                 Position = translation.Value,
    5.                 MaxDistance = 2.0f,
    6.                 Filter = CollisionFilter.Default
    7.             };
     
  2. NotaNaN

    NotaNaN

    Joined:
    Dec 14, 2018
    Posts:
    325
    You should be able to do something like this in an Authoring Component:
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3. using Unity.Physics.Authoring;
    4. using Unity.Physics;
    5.  
    6. // Example Component that holds a Collision Filter.
    7. public struct ExampleComponent : IComponentData
    8. {
    9.     public CollisionFilter CollisionFilter;
    10. }
    11.  
    12. public class ExampleAuthoring : MonoBehaviour, IConvertGameObjectToEntity
    13. {
    14.     public void OnEnable() { }
    15.  
    16.     public ExampleComponent ExampleComponent;
    17.  
    18.     public PhysicsCategoryTags BelongsTo = new PhysicsCategoryTags { Category00 = true }; // You can switch / add / remove the default "layers" here.
    19.     public PhysicsCategoryTags CollidesWith = new PhysicsCategoryTags { Category01 = true }; // You can switch / add / remove the default "layers" here.
    20.  
    21.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    22.     {
    23.         // Set the uint values on the CollisionFilter.
    24.         ExampleComponent.CollisionFilter.BelongsTo = BelongsTo.Value;
    25.         ExampleComponent.CollisionFilter.CollidesWith = CollidesWith.Value;
    26.  
    27.         // Add the component.
    28.         dstManager.AddComponentData(entity, ExampleComponent);
    29.     }
    30. }
    And that will give you two dropdown boxes for you to select your BelongsTo and CollidesWith on the CollisionFilter within the ExampleComponent.

    You can set the default values by initializing the PhysicsCategoryTags struct like I'm doing. Just make sure to set the defaults to values that make sense for your own project!
     
    kru and shotoutgames like this.
  3. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Also, you can do something like BelongsTo = SomeValue, CollidesWith = ~SomeValue (meaning it belongs to a certain group and collides with everything but that group).

    In the editor, just choose some category for Belongs To in the Collision Filter field of the Physics Shape script, and in the Collides With make sure this category is deselected.
     
    WAYNGames, shotoutgames and NotaNaN like this.