Camera Look System in Unity

In a 3D game when it is developed for pc the mouse is always associated with the camera control, my prototype, for now, simulates a third-person camera.

As the first thing, we must understand how to take the input related to the movement of the mouse. As for the movement of the player that uses the keyword “Horizontal” and “Vertical” to associate to the WASD commands, within the Project Settings under the Input Manager we can also find two keywords that relate to the management of the two axes of the mouse.

Now that we know their names “Mouse X” and “Mouse Y” we just have to link them to the code. But what we need to link them to is the second step. The vertical axis must rotate the camera up and down while the horizontal axis must move the camera rotation or modify the player rotation. In my case the right answer is the second one, it will be the rotation of the player to modify the horizontal rotation of the camera.

Let’s go to the code, the rotation to be modified requires the intervention of the Euler angles. In fact, it will not be as easy as modifying the position but not too difficult once you get the hang of it.
There is more than one method to solve this problem, I decided to use a Quaternion method that will modify the local rotation of the player for the horizontal movement and of the camera for the vertical one.

In addition to the vertical movement, I have added a constraint with the function Clamp to limit the vertical movement.

Last but not least I deactivated the mouse pointer on the screen when the game is active to avoid confusion and to refer only to the cross air pointer that I have on the screen.

project manager by day, unity developer by night