Collisions
This page describes how to filter and detect object collisions.
Collisions filtering
Flax supports up to 32 different collision layers. Each layer can have different a collisions mask defined.
You can use Physics Settings to define the layers collisions mask matrix. Every actor has property Actor.Layer which is used to peek the collisions mask for the object.
Collisions detection
Flax uses event-based collision detection. When a collision between two objects is detected it is reported during the fixed update. Use the Collider events to handle the collisions. To access information about the collision use Collision class and ContactPoint structures.
Here is an example script which registers collision detection for a given input collider in the scene.
public class MyScript : Script
{
public Collider TargetCollider;
public override void OnEnable()
{
TargetCollider.CollisionEnter += OnCollisionEnter;
}
public override void OnDisable()
{
TargetCollider.CollisionEnter -= OnCollisionEnter;
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("We got the collision sir! With: " + collision.OtherCollider);
}
}
Please keep in mind that not only Colliders may be a source of collision but also other actor types eg. Terrain. To handle this use properties ThisActor and OtherActor.
Learn more
See Script Events page to learn more about the C# script events.