Welcome to UnityConstraints!
A collection of 3D constraints for Unity game development
To use a constraint, simply add it to a GameObject by choosing a constraint from the menu:
Components > Path-o-logical > Constraints
You can add multiple constraints to a GameObject to create complex behaviors. For example, say you want to create a sprite with a "locked on target" icon texture that follows a target and always faces the camera. To do this, simply add a Transform Constraint, set to only position, and a Billboard constraint.
UnityConstraints packages now include Example files. You may choose not to import these by un-checking the corresponding boxes in Unity during the import process
"No Target" Modes
Most constraints have three options for what to do when there is no target given. This is especially handy when used through code where, at times, you may want to have no target for a constraint:
- Logs a nicely formatted error message telling you what constraint and what object is missing a target
- Do Nothing
- Does nothing (surprise!). This is similar to the behavior of a disabled constraint. Once a new target is provided the constraint will begin to function again. This can be used to avoid errors when you know you will be giving the constraint a target but have to wait for some reason.
- Return To Default
- Will cause the constrained object to return to its default state depending on the constraint. This is most useful on the Smooth LookAt Constraint, which will rotate back to local 0, 0, 0. Great if, for example, if you are setting up a turret and want it to return to its start position when there is nothing to fire at.
- Set By Script
- Allows you to take control of the constraint. For example, instead of giving the constraint a target, you can set its position directly using a Vector3. Each constraint will behave as expected. For example, a SmoothLookAt Constraint would turn towards this arbitrary point in space.
Most constraints have three modes which can be set in the inspector, or during run-time via script:
- Runs every frame. This is the default and usually what you will want to use.
- Executes the constraint only once (usually when the GameObject is instanced, but can be triggered again by setting the mode via a script). Think of this as a one-time alignment. A good example of this is spawning a sprite which displays a flash, such as part of an explosion. A flash occurs so quickly that setting the sprite to billboard only once will work fine and will save a few frames of extra computation.
- Turns off the constraint functionality.
Other Important Information
Enabling or disabling
the constraint component, or entire GameObject, will toggle the constraint. This means that the constraint will do nothing while disabled and will resume whatever it was doing before it was disabled once enabled again.
When constraints are not running they have zero effect on performance
because they utilize co-routines instead of Update().