Stride

OPEN / CLOSE
  • Features
  • Blog
  • Documentation
  • Community
(icon) Download

  • Discord
  • Facebook
  • Twitter
  • YouTube

LANGUAGE

OPEN / CLOSE
  • English
  • 日本語
    Show / Hide Table of Contents

    Triggers

    Beginner Designer

    If you set a collider to be a trigger, other colliders no longer bump into it. Instead, they pass through.

    The trigger detects when colliders enter it, which you can use to script events. For example, you can detect when a player character enters a room, and use this in your script to trigger an event. For more information, see Events.

    Note

    Character colliders can't be used as triggers.

    Create a trigger

    1. Create a collider.

    2. In the Property Grid, under the collider component properties, select Is Trigger.

    Select 'Is trigger'

    Detect trigger collisions

    You can see when something enters the trigger using the following code:

    // Wait for an entity to collide with the trigger
    var firstCollision = await trigger.NewCollision();
    
    var otherCollider = trigger == firstCollision.ColliderA
        ? firstCollision.ColliderB
        : firstCollision.ColliderA;
    

    Alternatively, directly access the TrackingHashSet:

    var trigger = Entity.Get<PhysicsComponent>();
    foreach (var collision in trigger.Collisions)
    {
        //do something with the collision
    }
    

    Or use the TrackingHashSet events:

    var trigger = Entity.Get<PhysicsComponent>();
    trigger.Collisions.CollectionChanged += (sender, args) =>
    {
        if (args.Action == NotifyCollectionChangedAction.Add)
        {
            //new collision
            var collision = (Collision) args.Item;
            //do something
        }
        else if (args.Action == NotifyCollectionChangedAction.Remove)
        {
            //old collision
            var collision = (Collision)args.Item;
            //do something
        }
    };
    

    For an example of how to use triggers, see the Script a trigger tutorial.

    See also

    • Tutorial: Script a trigger
    • Colliders
    • Collider shapes
    • Events
    • Improve this Doc
    In This Article

    Back to top

    Copyright © 2019-2021 .NET Foundation and Contributors
    Supported by the .NET Foundation