Nerd For Tech
Published in

Nerd For Tech

Camera Look System in Unity

I have my player moving and now I need the camera to follow them.

The first thing you can do is set your camera to where you want it and parent it to the player. This will get the camera to follow the player.

In the player script, you will want to set a reference to the main camera.

Create a new method to hold the code for the camera controls. Create two variables to hold the value of the mouse movement on the x and y axis.

To get the player looking left and right, create a new vector 3 that will be the player's transform.localEulerAngles. Set the y value of the vector 3 to be plus equals the mouseX input multiplied by a sensitivity value. Set the rotation of the player using Quaternion.AngleAxis(currentRotation.y, Vector3.up).

Note: you can make a sensitivity variable at the top of the script like the player speed variable

For the vertical camera movement, you do something very similar. The only two differences are that you will subtract the moveY input and you will clamp the x value so that they can only look up and down so far.

With this, you will also notice that the player is not moving the way the camera is facing.

This is because you need to convert the velocity of the player from local space to world space. This is done by using transform.TransformDirection().

This will fix that issue.

The last thing you can do is lock your cursor in the center of the screen like most shoot games will.

This can be done by using the CursorLockMode to lock the cursor when the game starts and allow the cursor to be unlocked by hitting the escape key.

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Mixed model pains of kubernetes ingress on AWS

What is a Relational Database

CS371p Spring 2021 Week 4

MinerBlox | Airdrop | Get Free 10 $MBLOX For Everyo

Embrace Request Changes

How to show Firebase Firestore Data in Flutter

Installing Python Dependencies in Dataflow

CSoD Micro Experience — Part I

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matthew Clark

Matthew Clark

Unity Game Developer and C# Programmer

More from Medium

How to Set Up a Simple Drop-Loot System in Unity

A quick guide to player setup in Unity

My journey becoming a Unity game developer: 2.5D Infinite Runner-Ledge Grab System-Pt3

Creating Rewarded Video Ads in Unity