A simple mouse orbit camera following the player.
In this tutorial we’ll create a schematic to control a camera following the player. The camera angle/orbit can be changed by holding the right mouse button and moving the mouse horizontally. Additionally, the camera can be zoomed using the scroll wheel.
You can download the finished schematic here.
Download Schematic
First, let’s take a look at the input keys we’ll be using. Open the Makinom editor and navigate to Base/Control > Input Keys.
We’ll need the mouse’s horizontal movement, the right mouse button and the scroll wheel – horizonal movement and scroll wheel will be coming from the Unity Input Manager, the default input setup in Unity contains the needed inputs (Mouse X and Mouse ScrollWheel).
If you don’t already have input keys like that in your project, add them and change the following settings.
That’s it for the input keys – click on Save Settings to save your changes.
Now, we’ll create the schematic handling the camera control. The schematic will use some object variables that we’ll add to the camera to control the horizontal offset and zoom.
Navigate to Schematics, create a new schematic and change the following settings.
Add Node > Input > Input Key
First, we’ll check if the right mouse button is held or the scroll wheel was used.
Change the already added input key.
Click on Add Input Key to add another input key.
Add Node > Value > Variable > Change Variables
In case the right mouse button is held down, we’ll use this node to add the mouse movement to the horizontal offset (stored in the float object variable yOffset). Since the mouse movement alone is pretty slow, we’ll first store it in a local variable and multiply it by 10 before adding it to the offset.
This node is connected to the Right Mouse Button slot of the Input Key node.
Click on Add Variable to add a variable change.
Copy the previous variable change and change the following settings.
Click on Add Variable to add another variable change.
In case the scroll wheel has been used, we’ll use this node to update the zoom target (float object variable we’ll use for smooth zooming).
This node is connected to the ScrollWheel slot of the Input Key node.
Add Node > Value > Float > Float Clamp
After updating the zoom target, we’ll clamp it between 2 values to add minimum and maximum zoom the player can reach. To make things easy, we’ll just define values here – feel free to use variables to easily adjust the zoom limits (e.g. using local start variables).
Add Node > Value > Float > Float Smooth Damp
We’ll use this node for smooth zoom changes from the current zoom to the zoom target.
Since we’ll do this in any case, this node is connected to the Failed slot of the Input Key node and the Next slots of the Change Variables node (right mouse button) and Float Clamp node (scroll wheel).
Add Node > Movement > Movement > Smooth Follow
We’ll use this node to update the camera’s position, using the horizontal offset (yOffset) and zoom.
As Y-Axis, we’ll use the zoom.
As Z-Axis, we’ll use the negated zoom.
Add Node > Movement > Rotation > Rotate To
We’ll use this node to let the camera look at the player. This will usually look at the feet of the player, but you can define a child object as target, e.g. the head or right shoulder.
That’s it for the schematic – click on Save Schematic and save it e.g. as MouseOrbitCamera.
Finally, we’ll need to add some components to the camera – the object variables component to store the used variables and a tick machine playing the schematic.
Select the camera’s game object in the scene.
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.
Again, copy the last variable and change the following settings.
Add a tick machine component to the camera (e.g. using the component menu: Makinom > Machines > Tick Machine) and change the following settings.
And that’s it!