Code Reference‎ > ‎

New Constraint Template

Below is a template for creating new constraints. The internal functionality is handled by base classes (see the Class Structure here).

To request a new or expanded constraint, please visit our forums and post your idea/need. We don't really expect users to create their own constraints. If you do create a new constraint on your own, we would love to see it, even privately via support@path-o-logical.com.


using UnityEngine;

/// <description>
/// This is a new constraint template
/// </description>
[AddComponentMenu("Path-o-logical/Constraints/New Cconstraint")]
public class NewConstraint : ConstraintBaseClass
{
    /// <summary>
    /// Cache as much as possible
    /// </summary>
    protected override void Awake()
    {
        base.Awake();

        // this.xform is available (cached transform component)
    }
   

    /// <summary>
    /// Runs each frame while the constraint is active
    /// </summary>
    protected override void OnConstraintUpdate()
    {
        // Add any code here to be run once in 'Once' mode, or every frame in
        //   'Constrain' mode.
    }
}


To create a LookAt-style constraint use this template but inherit from LookAtBaseClass instead, which includes the pointAxis and upAxis vector3s, for internal use, and this function which should always be used at the end of OnConstraintUpdate() to process the final rotation. Just solve for forward and up and then call DoLookAt() to factor in the users input from pointAxis and upAxis (you do not need to use these directly).

/// <summary>
/// Processes the user's 'pointAxis' and 'upAxis' to look at the target.
/// </summary>
/// <param name="lookVect">The direction in which to look</param>
/// <param name="upVect">The direction to be used by the up axis</param>
protected void DoLookAt(Vector3 lookVect, Vector3 upVect)