Custom Triggers
This section provides some direction for creating custom Impact Triggers. Impact Triggers are the most straightforward and flexible part of Impact CFX to extend. They don't require you to inherit from any specific class or interface, and the logic of the trigger is entirely up to you.
For comprehensive documentation of the entire Impact CFX API, please refer to the API Reference.
Example Custom Trigger
The following example shows a very basic custom Impact Trigger implementation:
using ImpactCFX; using UnityEngine; //Note we are not inheriting from any Impact CFX class. public class ImpactExampleTrigger : MonoBehaviour { //Inspector field for assigning an Impact Object to use when triggering the collision. public ImpactObjectBase ImpactObject; //Use the OnCollisionEnter message for this trigger. private void OnCollisionEnter(Collision collision) { //Get the first contact point. ContactPoint contactPoint = collision.GetContact(0); //Create an ImpactContactPoint that wraps the contact point obtained from the collision. //ImpactContactPoint consolidates 3D and 2D physics. //collision.relativeVelocity is given as a fallback velocity. ImpactContactPoint impactContactPoint = new ImpactContactPoint(contactPoint, collision.relativeVelocity); //Attempt to get an Impact Object component from the collider that was hit. //Note the use of the IImpactObject interface to keep things abstract. IImpactObject hitObject = contactPoint.otherCollider.GetComponentInParent(); //Queue the collision. ImpactCFXGlobal.QueueCollision( ImpactObject, //The object that is triggering the collision. hitObject, //The object that was hit. impactContactPoint, //The contact point of the collision. CollisionType.Collision, //The type of collision (Collision, Slide, or Roll) 1, //How many materials to get at the contact point for this object. 1, //How many materials to get at the contact point for the other object. CollisionVelocityMethod.RelativeVelocities); //The method to use for calculating the velocity. } }
Not much is required to make a basic custom trigger, but there are a few key things to take note of.
ImpactContactPoint
ImpactContactPoint is a data structure that wraps both ContactPoint and ContactPoint2D structures provided by Unity. This consolidates the two kinds of physics and makes processing collisions much simpler.
ImpactContactPoint has many constructors that cover a variety of use cases. In the above example, the constructor taking a ContactPoint as a parameter was used. The collision.relativeVelocity was also given, as this velocity is sometimes used in velocity calculations later.
ImpactCFXGlobal
ImpactCFXGlobal is the primary class for interacting with the active Impact CFX Manager instance. The method we are interested in is QueueCollision. This method is responsible for taking collision data and queueing it for processing into an actual effect.
QueueCollision has a few overloads depending on the use case. In the above example, the overload taking two IImpactObject parameters for the trigger and hit object was used. You don't need to worry about null-checking here, the Impact CFX Manager automatically does this when queueing.
Inheriting From Impact CFX Trigger Bases
While inheriting from any particular class is not necessary, there are several base classes for different kinds of triggers that may be useful to inherit. These classes may have shared properties or logic that may be useful.
- ImpactTriggerBase - The most basic trigger class, contains shared properties and logic for many built-in triggers.
- ImpactCollisionTriggerBase - Base class for one-shot Collision effects.
- ImpactSlideAndRollTriggerBase - Base class for Slide and Roll effects.
- ImpactOnTriggerEnterBase - Base class triggers that use OnTriggerEnter for generating one-shot Collision effects.
- ImpactOnTriggerStayBase - Base class triggers that use OnTriggerStay for generating Slide and Roll effects.
- ImpactSpeculativeCollisionTriggerBase - Base class the special speculative triggers for one-shot Collision effects.