A new project! 3rd Person Shooter needs a 3rd Person Camera!
Although one of my previous projects had been a 3D project, that game used static virtual cameras that would switch on triggers.
This time however I made a 3rd person camera that is controlled by the mouse! Now this isn’t quite right at the moment but we will get to that!
With our player able to move freely, lets take a look at the code!
Using Quaternions can be a little daunting just cause of the name but in reality it just stands for the rotation section of the transform component.
After setting our typical control GetAxis to float variables (this time our mouse movements), we then need to make sure that we can control our localEulerAngles(not world!) with them on each our mouseX and mouseY. It is flipped her due to the 3D so I am using mouseX for the y rotation and mouseY for the x rotation.
We can add a sensitivity variable as well so we can adjust the speed on the fly.
Lastly we set our local rotation to to a new rotation based on our respective mouse movement on the axis we choose. In the case of our currentRotation.y, Vector3.up represents the y-axis.
Repeat for the x axis (also while making sure to clamp the distance for this case).
After childing the Main Camera to the Player… we have a working camera look system that will actually rotate our Player as well!
The biggest thing is that we have to make sure both our movements for both the rotation changes are LOCAL.
We also need to make our Player move in LOCAL space as well by making sure our velocity uses the following code. With this our Player using the Unity Character Controller will move in the direction they are rotated in!
With all this put together…
I have successfully created a proper 3rd person movement system! Phew!
This project will just be a prototype so it may not take too long, however I am definitely going to be learning quite a bit through it.
See you next post!