Trigger/Action is an organizational strategy that loosely couples events to actions. It's what happens if you take single responsibility principle to its logical conclusion. Not only is code easier to work on, but it's faster to prototype in and easier to wire up.
Drop a trigger like KeypressTrigger onto a GameObject. Set ButtonName to "Fire1" if it isn't already. Drag a SpawnPrefabAction onto the same GameObject. Drag that action onto the trigger in the action slot. Assign a Prefab to the Prefab slot. Run the game and every time you press the left mouse button, the prefab will be spawned. Takes like 10 seconds when you're used to the system.
Drop the TriggerAction folder inside Assets/Plugins (Create Plugins if it doesn't exist.)
When a button is pressed down, fires off the attached action. Good for things like jump.
When a button is released, fires off the attached action. Good for things like clipping a jump short.
When an object enters the trigger, fires off the associated action. Remember that you can create custom layers in Unity3d that only react when a particular gameObject (like the player) enters.
Fires the action immediately when the gameObject fires its Start() callback.
Fires the action immediately when the gameObject fires its Start() callback.
Utility action that will call another action, but not fire again (no matter how many times it is called) until an amount of time has passed. Super useful for controlling player attacks.
Simply prints to the log. Useful for figuring out if a complex chain is working correctly.
Utility action that when called will wait a specified time, then fire another action. Useful for timing things like destroying a crossbow bolt 10 seconds after it's been spawned.
Freezes the game for a few frames when this action is hit. Useful for giving a feeling of weight to player hits.
Utility action that hides the cursor.
Utility action that will call another action, but just once, then silently ignore all future action calls.
Plays the attached AudioSource.
Crossfade queues up the specified animation.
Utility action that will fire the attached action a given percentage of the time. This is useful for introducing a little randomness into your game.
Repositions the named GameObject somewhere randomly between the specified MinPosition and MaxPosition.
Utility action that when called will randomly choose one of the attached actions, and fire it. This is useful for introducing a little randomness into your game.
Deletes a gameobject.
Deletes a number of gameobjects.
Utility action that can be used to call nearly anything. Easy to overuse.
Utility action that runs the specified other actions in turn.
Sets a target GameObject to active or disabled depending on the setting.
When the action is fired and the user is holding down shift, fires one action, otherwise fires another action. Perhaps a little overly specialized for this collection.
Spawns a specified gameObject. Options include:
AttachToSelf
-- boolean that specifies if this gameObject should be the parentIsTemporary
-- indicates the spawned object should eventually be deletedTemporaryLifespan
-- indicates how long the object should live
These last two settings are useful for one-shot particle emitters.
Utility action that alternates between firing one action or another. Useful for enabling and disabling menus.
Shows the cursor. Useful for enabling the cursor for menus after disabling it in game.
First, check out the source to the provided triggers and actions. They're not complicated at all.
A trigger is any class that takes an ActionBase and calls ActionBase.Act() on it.
Inherit from ActionBase and override the Act() method in your class with your payload code.
I hope you find this a useful way to organize your code. I used these triggers and actions in a number of my games including Exploding Kittens for iOS and Android.
Feel free to contact me if you have any questions, comments, potential contract work, or want to contribute anything back. I love pull requests.
Cheers,
Mike Judge
@mikelovesrobots on Twitter