Setting up the follow camera and background parallax.
In this tutorial we’ll create schematics to let the camera follow the player and handling the background parallax animation.
First, we’ll add object variables to the camera – we’ll use them to store the last camera position to use it in the background parallax to calculate the parallax effect due to the camera movement since the last frame. Select the mainCamera game object in the scene hierarchy and add an Object Variables component (e.g. using the component menu: Makinom > Scenes > Object Variables).
Change the following settings of the object variables component.
Click on Add Variable to add a new variable to the object variables. We’ll store the camera’s current position as an object variable.
Now, we’ll create the schematic to let the camera follow the player. Additionally, the camera will also stop at the edges of the level.
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
We’ll set up a local variable as Machine Start Variable for easy setup in the machine component at a later time. When using the schematic in a machine component, the defined start variables will be added automatically, using their default values.
Click on Add Start Variable to add a local start variable that will be exposed to the machine component’s inspector. We’ll use the variables to limit the camera’s area.
Click on Add Start Variable to add another variable.
Add Node > Movement > Movement > Smooth Follow
We’ll use this node to let the camera follow the player.
Add Node > Movement > Movement > Clamp Transform
We’ll use this node to force the camera to stay within a defined area, i.e. it will stop following the player when reaching the edges of the level.
The min/max values we’ll use for that will be set by the machine playing the schematic as local start variables.
That’s it for this schematic – click on Save Schematic and save it as CameraFollow in Assets/Schematics/.
Next, we’ll add the schematic to the camera using a tick machine. We’ll stop the camera from following the player when the player died through changing the In Control game state.
Add a tick machine component to the mainCamera game object (e.g. using the component menu: Makinom > Machines > Tick Machine) and change the following settings.
Click on Add Condition, we’ll set up the game state condition here.
In the added Condition 0, we’ll set up our game state condition.
Click on Add Game State.
Before we’ll create the schematics for the background parallax, we’ll set up a formula to calculate the parallax factor of the current background level – this will determine how much the background will be moved. We could also do this in a schematic – but why not use a formula this time?
Formulas are used to calculate float values and can be used in every float value field. A formula can use 2 game objects – the user and the target of the formula, which can be used for distance checks or as variable origins – and shares local variables with the schematic that uses the formula. This allows a formula to return different results depending on the used game objects and schematics.
Like schematics, formulas are created in a node editor. Each node will either manipulate the current value of the formula (e.g. adding 1 to it, multiplying it using a local variable) or do a check to determine the next node.
Now, open the Makinom editor, navigate to Game > Formulas and change the following settings in the default formula.
Like the Settings node of a schematic, this node contains the formula’s base settings.
We’ll be setting the start value of the formula using a local int variable that’s passed on from the schematic. The parallaxLevel defines the current level of the parallax we’ll be calculating the formula for – in the end, this influences how much the background is moved based on the parallax level.
Add Node > Value > Value
This node is used to change the current formula’s value with another value. We’ll use it to multiply the start value with the parallaxReductionFactor (local start variable from the schematic).
Next, we’ll just add 1 to the formula value.
Finally, we’ll multiply the value again, this time with the parallax calculated from the previous camera position in relation to the current position (this will be done in the schematic).
That’s it for the formula – click on Save Settings to save the settings.
Next, we’ll create a schematic that will handle moving a background level using the formula we just created. This schematic will be started by another schematic for each background game object that will be moved in the parallax animation – we wont use tagged machines for this, but directly start the schematic using a Start Machine node.
Navigate to Schematics, create a new schematic and change the following settings.
Add Node > Value > Variable > Change Variables
We’ll use this node to set the new position we’ll be using.
Click on Add Variable to add a variable change.
Click on Add Variable again to add another variable change. Now we’ll add the result of the formula to the X-axis.
Add Node > Value > Vector > Vector3 Lerp
We’ll use this node to fade the background’s current position to the new position over time – using smoothing that we’ll define as a local start variable in the machine.
These settings handle where we’ll store the result of the lerp.
These settings define from which Vector3 we’ll start.
These settings define the target of the lerp.
These settings define the time used for the lerp – we’ll use this to smooth the lerp.
Add Node > Movement > Movement > Change Position
We’ll use this node to set the background’s position.
That’s it for this schematic – click on Save Schematic and save it as ParallaxBackgroundLevel in Assets/Schematics/.
This schematic will be started by a tick machine on the camera and starts the BackgroundParallaxLevel schematic for background game objects added as actors. To make things easy, we’ll add the various backgrounds as a single actor – the Start Machine node we’ll use to start the schematic on the background objects allows us to set an int variable as counter of started machins, which we’ll use to set the parallax level.
In short – much magic happening in a single step!
Create a new schematic and change the following settings.
We’ll set up some start variables and add a single actor – the actual game objects used by the actor will be added in the machine component later.
Click on Add Start Variable to add a local start variable that will be exposed to the machine component’s inspector.
Copy the previous variable.
Copy the previous variable again.
Click on Add Actor to add an actor.
We’ll use this node to set the parallax float variable we’ll use in the formula – this will use the previous camera position in relation to the current position to set a parallax factor.
Copy the previous variable change and change the following settings in the new variable change.
Again, copy the previous variable change and change the following settings in the new variable change.
Add Node > Machine > Start Machine
This node is used to start schematics on game objects without going through a tagged machine like we usually do. The Start Tagged Machine node is used for cases where you want to start something on another game object, which depends on the game object that is used – e.g. a tag could start an explosion on one game object and nothing on another.
The Start Machine node however will start the used schematic on each used machine – i.e. each combination of selected machine object and start object will be used. Additionally, we can set an int variable as counter for started machines – the 1st started machine will have counter 0, the 2nd started machine counter 1, etc. – this allows you to do different things in the started schematic based on which number is running. In our case, we’ll use this to set the parallaxLevel, which influences how big the position change of the background object will be.
Let’s get to it!
Finally, we’ll store the current camera position into the previousCamPos variable to use it in the next frame.
And that’s it for the schematic – click on Save Schematic and save it as BackgroundParallax in Assets/Schematics/.
We’ll add the background parallax schematic to the camera using a tick machine. Add a tick machine component to the mainCamera game object (e.g. using the component menu: Makinom > Machines > Tick Machine) and change the following settings.
We need 4 game objects for Actor 0, so add the needed amount.
Select the following 4 game objects in this order (they’re child objects of the backgrounds game object in the scene hierarchy):
And that’s it – don’t forget to save the scene.
Click on Play to test the game. The camera will now follow the player around and stops at the edges of the level – also, the background will slightly move in a parallax effect when the camera moves left or right.
The next tutorial will handle shooting rockets.
© 2015 Gaming is Love e.U.
Disclosure: This site may contain affiliate links, which means I may receive a commission if you click a link and purchase something that I have recommended. While clicking these links won’t cost you any money, they will help me fund my development projects while recommending great assets!