Machines are what drives your game – learn the basics about those components!
A machine is a Unity component that plays a Schematic – in short, a schematic is a blueprint for what you want to do, and a machine makes it happen. You can learn more about schematics in this how-to.
There are different machine components available for different use cases. Each machine component has it’s own how-to with more details, so let’s just get a brief overview.
- Animation Machines
Started by animation callback functions, e.g. when the internal IK of an animator is updated.
You can learn more about animation machines in this how-to.
- Animation State Machines
Started by Mecanim animator controller state changes (StateMachineBehaviour), e.g. when entering a state or state machine. This machine component is only available for animator controller states and not for game objects.
You can learn more about animation state machines in this how-to.
- Application Machines
Started by application callback functions, e.g. when losing focus or pausing the game.
You can learn more about application machines in this how-to.
- Auto Machines
Started by creation or destruction of game objects, e.g. when a game object is instantiated, enabled or disabled.
You can learn more about auto machines in this how-to.
- Collision Machines
Started by game objects colliding with the machine, e.g. when a game object starts or stops colliding with the machine.
You can learn more about collision machines in this how-to.
- Interaction Machines
Started by player interaction, e.g. when clicking on the machin by mouse/touch or the pressing the Interact Key.
You can learn more about interaction machines in this how-to.
- Network Machines
Started by network callback functions, e.g. when the client connects to a server.
You can learn more about network machines in this how-to.
- Render Machines
Started by renderer and camera callback functions, e.g. when the machine becomes visible or invisible.
You can learn more about render machines in this how-to.
- Tagged Machines
Started by schematics via tags, e.g. a schematic can start all machine tagged ‘explode’ in the scene.
You can learn more about tagged machines in this how-to.
- Template Machines
Adds a machine component based on a template setting.
You can learn more about template machines in this how-to.
- Tick Machines
Started by update functions, e.g. every frame (Update) or fixed framerate frame (FixedUpdate).
You can learn more about tick machines in this how-to.
- Trigger Machines
Started by game objects in the machine’s trigger, e.g. when a game object enters or leaves the trigger.
You can learn more about trigger machines in this how-to.
Beside machine components, there are Global Machines that can be used everywhere and anytime. You can learn more about global machines in this how-to.
The machine component’s Machine Execution Settings defines what and how the machine will execute. Machines can execute schematics or timelines.
Starting a schematic requires a schematic asset. Additional settings further specify the machine’s execution, which influence the impact/effect of the schematic’s execution individually and in context to other machines.
The machine’s Execution Type defines how a machine is executed:
The machine can be executed once at the same time.
I.e. it can’t be executed again while it’s already running.
The machine can be executed multiple times at the same time.
The machine can be executed once at the same time.
Other Blocking machines can’t be executed while the machine is running and the machine can’t start while another Blocking machine is running.
The Update Type defines in which update function the machine will be updated:
This is the default update function and should be used in most cases.
- Late Update
Each frame after Update.
Best used for camera related things, e.g. when adjusting the camera to the player’s position.
This is due to the fact that Late Update is executed after Update, i.e. when the player has already been positioned for the frame.
- Fixed Update
Each fixed framerate frame.
Best used for physics related things, e.g. when dealing with rigidbodies.
You can optionally prioritize a machine, this will update a machine before other machines. When multiple machines are prioritized, they’re order descending by their Priority number, i.e. the machine with the highest priority number will be updated first.
However, prioritized machines will start playing the schematic in the next frame, not in the frame they’re started (as not prioritized machines do). This is due to waiting for all prioritized machines that are started in a frame and ordering them according to their Priority number. I.e. a priority machine ensures starting before other priority machines with a lower priority.
E.g. 3 auto machines start when a scene is loaded:
- machine A, no priority
- machine B, priority 0
- machine C, priority 5
The machines will start playing the schematics in the following order:
- machine A
- machine C
- machine B
Instead of playing a single schematic, a timeline can start multiple schematics, tagged machines and global machines based upon points in time relative to the start of the timeline’s machine. E.g. point 0 is the moment the machine starts, point 1.5 is 1.5 seconds after the machin’s start. Timelines can be used to create complex, time driven events – like cut-scenes or a nice series of explosion effects.
Timelines aren’t available for animation state machines or template machines. You can learn more about timelines in this how-to.
If a machine is started is depending on the selected start type (there are different start types available in the different machine components, e.g. a trigger machine can start upon Trigger Enter, Trigger Stay and Trigger Exit) and optional conditions that will be checked. While some machine components have additional conditions that can be checked, the following conditions are available for most machines.
A machine can be started depending on the position of the machine and the starting object. You can check if the starting object is in front, back, left or right of the machine. E.g. you can make an interaction machine (e.g. opening a door) only able to start when the player is in front of the door.
This condition is not available for animation state machines.
Game State Conditions
Starting a machine can depend for the game to be in defined states. The game can have multiple active game states at the same time – states can be changed automatically upon certain events (e.g. blocking the player control) and through schematics. You can learn more about game states in this how-to.
An interaction machine (e.g. displaying a dialogue) is only able to start while the game state In Control is active, which is set to only be active while the player can be controlled. This way, the interaction will only be available while the player is in control, i.e. it can’t be started during blocked control (e.g. in a cut-scene).
Starting a machine can depend on variables having certain values – variables and variable conditions allow creating advanced and complex game mechanics. You can learn more about variables in this how-to.
A trigger machine (e.g. starting a cut-scene or auto saving the game upon entering the trigger) is only able to start when the bool variable quest_A_started is enabled. The variable is enabled by another schematic, e.g. through a dialogue with an NPC.
Local Start Variables
A machine can initialize the local variables of the schematic it starts to defined values. While you can also set them in the schematic, setting them in the machine allows reusing the schematic with different start variables, doing different things or behaving differently.
An auto machine creating a tiled level with a random layout gets the level’s size from the local variables. Using two int variables (e.g. columns and rows) to define the size, you can create different level sizes with the same schematic. Level 1 can have a size of 8×8, level 2 a size of 10×8, etc.
A trigger machine will damage the player (reduce the health variable of the player) when he enters the trigger. Using an int variable (e.g. damage) to define the damage that will be done will allow using the scame chematic to do different damage on different machines.
Change After Execution
Machines can automatically change variables after they finished execution. While you can also change variables in schematics, doing this in the machine allows reusing the schematic for different purposes and changing different variables.
A trigger machine will add points to the player’s score (e.g. increase an int variable score) when he enters the trigger. To only get the points once, change the bool variable points_A_collected after execution to true and use a variable condition checking for that variable to be false. Using a different variable for each points collection will make them unique and only be collectible once. Changing the variable in the schematic would change the same variable for all machines, requiring to have different schematics for every individual points collection.
A tagged machine will remove (kill) an enemy game object, playing an animation and an audio clip. The resource overrides allow reusing the schematic for different enemies and using different audio clips. To add a kill counter for different enemies (i.e. keep track of how many enemies of different types you’ve killed), increase different int variables for different enemies (e.g. enemy type A increases enemy_A_counter, enemy type B increases enemy_B_counter).