[Unity3D] - A simple third-person camera controller
Today we want to share with you about the third-person camera controler we have developed for our new game Boliscape. We wil not cover the source code of the camera controller in detail but we are making the code available to you (1 file ~150 lines of code). It comes with no license at all. Feel free to do whatever you want with it for your commercial and non-commercial projects.
If you have worked with 3D before, surely you have faced how hard can it be to make a camera controller generic enough so that, during the gameplay, the camera always records the player from the best point of view. By best point of view we mean a view where:
1 - The player is not being occluded entirely by others objects of the scene and the view is relevant enough to the user success in the game;
2 - The camera is not penetrating any object of the scene (that's what we want most of the time to happen)
In our game Boliscape we want the user to control the camera with a joystick, so our camera controller does not takes care of point [1]. In fact, using the joystick, the user will attempt to naturally find the view that is more confortable with to carry his missions in the game. However, our camera controller takes care of point [2]. This means that, by default, the camera is not allowed to penetrate the objects of the scene. We will come back to this point in details later in this article. Let's first look at a diagram that shows how the camera and the player are related:
Our camera controller ensures that the camera always try to follow the player across the scene by keeping a default vertical and horizontal distance to him. These default two distances can be set in the editor when editing the camera controller component. However, as we will see later, these two distances can increase or decrease during the game play if the camera is being blocked by another object in the scene (due to an imminent penetration). In all circuntances during the game, the right vector of the camera is always paralel to the ground (plane XoZ). Before going further, let's have a quick overview of an interaction with the camera joystick (in our game, the camera joystick is the right one):
As we can see from the video above, interactions with the camera joystick do not induce changes in the horizontal and vertical distances. In fact, the vertical axis of the joystick, is used to look up or down. This is accomplished by simply making the camera rotate along its local right axis. On the other hand, the horizontal axis of the joystick is used to look to the left or to the right. Looking to the left is achieved by rotating the camera to the right. The axis of rotation used is the vertical axis (up vector) centered in the player (ball in our game). With that said, you may understand that moving the joystick up or down, does not change the camera's world position while moving the joystick left or right changes the camera world position. So, in our rendered screen, the player always appears with the same horizontal position (middle of the screen) while its vertical position changes acording to the joystick's vertical axis value.
As we said before, by default, the camera does not penetrate the objects of the scene and, to fufill this condition, the camera adapts its horizontal and vertical distances to the player. Let's understand this better with an example:
As we can see in the video, when the user moves the joystick to the right, the camera rotates to the left and because of the left brick wall, the horizontal distance from the camera to the player (ball) is gradually reduced. However, when the user moves the joystick to the left, the brick wall is no longer behind the camera and the camera starts to restore gradually the horizontal distance to the player to its default value. This behaviour we have just covered also applies to the vertical distance. The camera can restore to its default horizontal and vertical distances slowly or quickly. The restore velocity can be ajusted in the editor using field RestoreToDefaultLinearVelocity. Still in the editor, the joystick's horizontal and vertical axis sensitivities can be ajusted using fields LocalAngularVelocityAroundUpVector and LocalAngularVelocityAroundCameraRightVector, respectively.
As we said before, by default, our camera controller does not allow the camera to penetrate any object (any object with a collider). However, in some cases, we may want the camera to penetrate specific objects. Here is an example, where we want the camera to penetrate the red barn wall:
How can we specify this behaviour to happen for specific colliders? Well, it is easy, field AllowedCollidersToBePenetratedInScene is a list that you can fill in the editor with all the colliders from the scene you allow to be penetrated.
With all we have covered so far let's see what happen in the Boliscape's levels "Old Gold Mine" when the ball goes inside the mine:
As you can see, because the camera is not allowed to penetrate the rocks of the mine, the camera gets stuck at the entrance of the mine. During this time, the horizontal distance increases continuously but the vertical distance stays constant. Also, at the end of the video, we can see what we have covered before: the camera gradually reduces its horizontal distance to the player until its default value is reached.
But how can we make the camera go inside the mine and follow the player there? It is pretty easy: you just have to create a new box collider at the entrance of the mine like this one:
Make sure to specify this collider as a trigger collider so that it is not taken into consideration for rigid bodies physics simulation purposes. This collider makes the camera gradually reducing its vertical distance to the player so that at the end it reaches a vertical distance low enough to fit inside the mine. See by yourself:
As you may understand, the beauty of this solution is that you do not need to program the entrance of the camera into the mine. You just need in the editor to add this trigger collider to the list TrigerCollidersToNotBePenetratedInScene and the camera controller takes care of the rest!
Well that's all we have to share today about the simple third-person camera controller we have developed for our game Boliscape.
If you want to try the camera controller by yourself in a published game just download Boliscape in google play.
You can find below a link to Boliscape, a link to the joystick package from the unity asset store we are using in CameraController.cs and also a link to CameraController.cs itself. Remember to attach the script directly to your camera game object.
Boliscape - the ball escape game: https://play.google.com/store/apps/details?id=com.xelart_game_studio.boliscape
Joystick asset: https://assetstore.unity.com/packages/tools/input-management/joystick-pack-10763...
CameraController.cs: https://1drv.ms/u/s!AiSRRDHwssaags02KICFnobzp2Yl7A?e=07lAnY
Get Boliscape - the ball escape game
Boliscape - the ball escape game
Xelart game studio brings to you the ultimate ball escape game.
Status | Released |
Author | Xelart game studio |
Genre | Puzzle |
Tags | Brain Training, logic-puzzle, physics-based |
Leave a comment
Log in with itch.io to leave a comment.