Unity Creating First Person and Third Person Cameras
Adding a simple tracking camera to your scene is not too hard, adding a first or third person camera is not more difficult.
First you add your player to the scene, we are using a legally distinct firefighter, then you make the camera a child object of the player like this.
then you need to move yourself into the head of the player and hit control+shift+F this will move your main camera to your current viewports position, don’t worry if it is not 100% accurate to where you want it.
Once you get it into this position you can fine tune a bit in the inspector. Next click on your main camera in the hierarchy.
you will see a pip mode show up in the bottom right, using this in the inspector you can fine tune the position of your main camera till you get the view how you want, also you can alter the near clipping plane of the camera so that you are no longer seeing any of the face parts.
You should be able to end up with something like.
Now you won’t have the ability to go up and down yet, so lets add that functionality.
Make a script and attach it to your player object under your update method you want to add a new method call lets call it CameraControl().
We will want a wait to track the two mouse axis we care about X and Y so make some variables that track these as floats.
float mouseX = Input.Getaxis(“Mouse X”);
float mouseY = Input.Getaxis(“Mouse Y”); are the two I am using for this.
We then need to get the current player rotation so
Vector3 currentRot = transform.eularAngles;
now since we don’t need to limit our horizontal mouse movement as we want to be able to spin around, we then add our mouse x input to our currentRot variable, since it is a vector3 we can add it directly to the y value with currentRot.y, we can multiple this by a extra int value to give us faster movement , mouse sensitivity in most games.
then we add that to our local transform to rotate on the axis, using transform.localRotation = Quaternion.AngelAxis() for this we just vector3.up for our axis we rotate around. This rotates our entire model and just with it.
For the vertical movement we need to add a little bit more, first we need to our current camera rotation to a variable.
then we need to clamp our vertical rotation, as we don’t want to be going to high or low.
so you should end up with something like this.
I have added it all in a if statement so you need to be holding the right mouse button to move the camera.
To make a third person version of the camera is the exact same, just the starting point for the camera can be further back rather then inside the head.