Setting up the player’s movement controls and camera.
In this tutorial we’ll do the basic player setup and create a schematic to control the movement. Additionally, we’ll let the camera follow the player’s movement.
The player’s basic setup involves adding object variables to store the player’s attributes and adding a simple schematic to set the player. Select the Player game object in the scene (it’s an already added instance of the Player prefab found at Assets/Tutorial Resources/Prefabs/) and add the following components.
First, we’ll add object variables to the player’s game object. 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. This variable will be used to store the player’s health. When the health reaches 0, the player’s ship will be destroyed.
Again, click on Add Variable. This variable will be used to store the player’s fire rate (i.e. time between shots).
Now, we’ll use a schematic to set the player when the level starts and add it with an Auto Machine component.
Follow the steps in this schematic tutorial – it explains how to create the schematic and add an auto machine to the game object.
Next, we’ll create a schematic to handle the player’s movement control. Open the Makinom editor, navigate to Schematics and create a new schematic.
The player’s game object will be moved using the Rigidbody component’s MovePosition function. Additionally, we’ll play/stop the walking animation in the game object’s Animator component (Mecanim).
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.
Add Node > Movement > Movement > Move
This node is used to move a game object each frame. We’ll use it to move the player using it’s rigidbody component.
The speed will be defind by the local start variable speed.
Add Node > Movement > Rotation > Rotate To
This node is used to rotate a game object toward something (i.e. look at). We’ll use it to rotate the player toward the mouse cursor.
This settings define how the mouse cursor position in world space will be found.
Add Node > Movement > Movement > Check Speed
This node is used to check the movement speed of a game object. We’ll use it to determine if we’ll play the walk animation.
Add Node > Animation > Mecanim > Play Mecanim Animation
This node is used to play an animation or set parameters of an animator. We’ll use it to play the walking animation of the player by setting the IsWalking bool parameter.
This node is connected to the Success slot of the Check Speed node.
Click on Add Parameter to add an parameter that will be changed.
Copy the previous Play Mecanim Animation node and connect it to the Failed slot of the Check Speed node.
We’ll use it to stop the walking animation – change the following setting.
And that’s it for the schematic – click on Save Schematic and save it as PlayerMovement in Assets/Schematics/.
The player is moved using a Rigidbody component, i.e. we’ll need to Fixed Update for the physics updates.
Add a Tick Machine component to the player’s game object (e.g. using the component menu: Makinom > Machines > Tick Machine). Change the following settings.
We’ll only want the controls to work while the player is alive, i.e. we need to check the health of the player using our template.
Click on Add Condition, we’ll set up the variable condition here.
In the added Condition 0, we’ll set up our variable condition.
Click on Add Variable.
The Machine Start Variable we’ve set up in the schematic is automatically added here with its default values. Make sure the variable is enabled.
And that’s it for the player for now – apply the changes to the prefab by clicking on Overrides > Apply All (top of the inspector).
We want the camera to follow the player’s movement, so we’ll set up a simple schematic to do that.
Open the Makinom editor, navigate to Schematics and create a new schematic. Change the following settings.
Add Node > Movement > Movement > Smooth Follow
This node is used to let one game object follow another game object each frame. We’ll use it to let the camera follow the player.
And that’s it for the schematic – click on Save Schematic and save it as CameraFollow in Assets/Schematics/.
Finally, we’ll add the CameraFollow schematic to the camera using a tick machine. Select the Main Camera game object in the scene hierarchy.
Add a tick machine component to the camera’s game object (e.g. using the component menu: Makinom > Machines > Tick Machine). Change the following settings.
And that’s it for now – don’t forget to save the scene.
Click on Play to test the game. You’ll be able to move the player around using the arrow keys and change the rotation using the mouse cursor. The camera will follow the player.
The next tutorial will handle the player’s weapon control.
© 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!