top of page
Search
Writer's pictureKieran Boyce

Third Person Camera

Updated: Mar 28, 2020

For this task, I had to make a better third person camera for the avatar that I had previously created in the beginning of the project. What I had done originally was parent the camera to the player. But now I’m going to try something different.


First of all, I’m going to create an empty object and place it inside the player, this is what I will want my camera to be looking at when the player is running around. Next add camera base to place the camera in this makes it easier to find things.





Next, I create a script which I then add to the camera base called followcam. this is where the code for the camera will be.


in this script, I need variables for things like smoothing the camera, the rotation speed, camera sensitivity and the Inputs, etc so i create them and make them public so they can be edited in the inspector.


in the start function i set the rotations for the camera, then jump into the update function which is where the bulk of the stuff is. First is set up the Inputs which i set up to be the controller right stick (although i forgot to rename them so they are still called mouse x and y). then set the x and y rotations to be equal to the inputs multiplied by the sensitivity multiplied by delta time that then gives the camera movement.




I then set a clamp on the x rotation so that the camera will stop rotation at a certain angle this can be changed inside of the inspector as I made it a public variable. Finally, I set up a function which quickly updates the camera's position and placed it in the late update function so that it is called every frame.




That is the basic camera done but let’s add some extras first of all i want to add in a function so that I can snap the camera in any direction. this is simple to do as I can create a variable for the position and then a switch statement which sets the rotation to 0,90,180 or 270 degrees allowing for the camera to snap to those directions.



I also wanted to add collision to the camera so that it doesn't just phase through everything so I created a new script for this. In this script I didn't have to do a lot I basically just had to get a ray cast hit to react when the camera collided with the wall and when it did so it would zoom in on the player. it mainly worked fine however there were still a few instances where it would clip through walls at some angles.


The camera feature worked out quite well especially after updating my player avatar. the camera rotated in all the ways that it was supposed to using the right stick quite well and my snap to axis feature worked without any issues as well it all moved quite well.


I did have one small bug with my camera collision as there were a couple of walls that it did still clip through. Other than that if anything my camera lacked a bit of polish, for example, the sensitivity wasn't the best as I had it set for a Playstation controller however when being tested with an Xbox controller it caused the sensitivity to slow right down which created a tedious experience for the player. I did have an option to change it in the inspector but maybe I should have implemented something in the game where the player could adjust it to their liking.



4 views0 comments

Recent Posts

See All

Comments


bottom of page