We’re currently being taught to write our own camera look system from scratch. To begin, a very simple way for the camera to follow the player is simply make it a child of the player.
As you’ve probably noticed, I’ve already set the cameras position and rotation to be in a place that’s close to ‘over the shoulder’ in most popular games. Here’s the stats:
Ok, we have have simple camera follow system! See you tomorrow! Ok, just kidding. There’s quite a bit more we’ll need to do…
Next, we’ll want to implement a camera look system. If we move the mouse left and right, we’ll have the player rotate, but if we move the mouse up and down, we’ll have the camera rotate up and down.
We’ll create our camera controller in our player script While we could create its own script, since it’s a child of the player, it will be easier to just leave the code in the player. First, let’s create a holder for the camera.
It’s best practice to create a null check for every object you create holders for.
let’s create our inputs for our mouse:
Now to have our character look left and right:
We can just rotate our player using our standard transform.rotate, though Unity will remind us we’re using Quaternion Transforms. If we use normal Quaternions, we risk something known as gimbal lock, and that’s when the axises rotate to their maximum and you lose one axis’s freedom of rotation.
Instead, we’ll use Euler angles which are basically Vector3s (that are easier to understand and work with) that are converted by Unity into Quaternions.
So here, we’re changing in real time in localEulerAngles x, y, and z and plugging in:
transform.localEulerAngles.x, which basically says “The X axis is what it is”
transform.localEulerAngles.y + mouseX, here we’re going to add to the y rotation whatever the mouses x input is.
And Z is the same situation as x. it is what it is.
So if we run it, this is what happens when we move the mouse:
Great! We have a rotating player and the cameras coming along, and while this is fine, there’s still a chance we’ll run into gimbal lock. We’ll need to implement a new Quaternion angle in order to avoid that, the line of code is also quite long, we can actually put our transform into a holder called currentRotation and then change that:
This is still basically the same code as before, except we’re converting the angles into Quaternions.
Let’s run it:
Works exactly the same! Let’s move on to up and down:
We’ve already created a holder for our camera, _mainCamera so we’ll be modifying its x rotation similarly.
Just like before, we’ll create a holder variable for the cameras current rotation, and we’ll be using Eulers.
Now that we have the cameras angles at the ready, we’ll want to modify the x angle:
Finally, we put our holders modified rotation info into the actual camera using Unitys Quaternion magic:
While the up and down is working as we intended, there’s something strange going on with the left and right movement now. What was ultimately discovered is that the player is being rotated in world space and we need to be rotating them in local space. Fortunately, it’s an easy fix and that’s just change the rotation to localRotation:
In the look left and right code:
Everything is now working as it should!
The next challenge is having the player move where the camera is pointing. Right now, we can face a corner, but we’ll still move forward, and that’s because we’re moving in world space. We want a way to move in the local space the camera is pointing in:
A google search finds us TransformPoint which will convert world to local, but we want the opposite. With a little searching, there is a method called Transform.TransformDirection() which will do the job!
So we essentially just need to convert our velocity from local (facing the corner) to the world space.
Excellent!! We now have a fully working camera system! Except it turns really slow. Let’s add some camera sensitivity. We’ll create variable for it:
And all we need to do is multiply it with our mouseX and mouseY inputs:
Let’s also turn off our cursor and turn it back on when the player hits escape:
Seeing it in action, we now have a perfectly good and working camera follow system that we wrote completely ourselves (with the help of GameDevHQ)!