The idea of that camera system comes from the game JumpKing where the camera changes as soon as the player exits the view of the camera.
At the beggining I wanted to recreate that system but instead I managed to create two different systems. One that works by direction, and the other that works with the distance.
My first thought was to use triggers but I knew from other projects that it can cause problems with layers or optimization. So I wanted to avoid triggers and colliders at all cost.
                                  ​​​​​​​
Similarities and node system
Both of those systems work with nodes that you can place anywhere, and link them for the direction system.
Currently you have to give each node their corresponding up, right, down, and left nodes by a simple drag and drop, and it's not easy. But I plan to make it easier by making it possible to select the direction and then select the node in the scene.
Those nodes contain all the information needed to set the bounds they affect, the position the camera will have, as well as the other nodes it is linked to.​​​​​​
I made the bounds visible for each node in the editor.
When the Direction mode is used and the application is playing, only the active nodes are shown and their colors are based on the direction they are from.
To move the camera, there are 3 different modes.
     - Instant
     - MoveTowards
     - Lerp
For both the Distance mode and the Direction mode, the movement system is the same.
For the instant mode, I simply set the position of the camera to the node camera position.
And for the MoveTowards and Lerp, I use the Vector3 methods to move the camera to the node camera position.
Both modes have methods to change the mode of the camera, the movement mode of the camera, the speed of the camera, and the target.
                                  ​​​​​​​​​​​​​​
Direction mode
This mode will move the camera to the node linked to the current node in the direction the target left.
The variables needed for this system to work are : the current node, the target, the camera and the last direction (an enum with the up, right, down, left, and none values).
This mode works like this :
     - Check if the target is in the bounds of the current node.
     - If the target is not in the bounds, it then tries to find the direction where the target left.
     - With that direction I can retrieve the node linked to that direction and I stock it in a buffer.
     
     - When the target enters the bounds of the node in the buffer, I move the camera to it.
            - In the case the target goes back to the current node, the buffer is emptied and the system resets.
     - Finally, the system will change the current node and reset the buffers and enums to their default values.
                                  ​​​​​​​
Distance mode
That mode will move the camera to the closest node to the target.
The main logic behind this mode is that instead of having a single node and looking to which ones are linked to that one, there is a List with every node in the game and it looks to find the closest one when the target has left the current node.
This one is easier to setup but harder to find ways to optimize, because this List could potentially be gigantic.
That mode works the following way :
     - If the user activates the alwaysCheck boolean, or if there is no currentNode, or if the target is not in the bounds of the currentNode, it will search for the closest node.
     - Then it loops through the entire nodes List and tries to find the closest one to the target.
            - Note that it return the first node if the target is inside its bounds to optimize a bit.
     - That returned node is made now the new current node.
     - The camera moves towards that node.
As this mode uses a List with all the nodes, it was important to have methods to modify that List.
So I made all the required methods to edit this List and made them Public.
I also made it so there won't be multiple same nodes in the list, and made some safety checks in case the user makes a mistake.
                                  ​​​​​​​
Custom Editor
Because this system has two modes, the nodes also need to be able to work for both modes. Luckily for the Distance mode there is not much to do. Simply show the correct parameters in the inspector.
Now for the Direction mode it gets a little bit trickier. This system works by having 4 fields of CameraNode for each direction. Without the custom editor you have to drag & drop each node to the correct field and you can get lost really fast and make mistakes.
So what I did was to have selection Toolbar for each direction and when the Edit nodes button is active I simply check when the user is clicking if it's mouse position is inside of a NodeBound. If it is, I then change the value of the direction node to the node selected.
I also added the delete buttons for each direction if you no longer need it setup.
There is also the custom bounds and the camera offset feature for both modes.
Finally I added a sphere gizmos to select more easily the nodes and see where they are. It can be hidden and it's color can be changed for each node.
Back to Top