Creating a Third Person Camera (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to look at how to create a third person camera we'll look at how to have the camera orbit around the character how to zoom in and out and how we can make the character move in the direction the camera is facing okay we've got a scene with a character that can run around based on player input the character has a character controller component attached that's used to move the character around and handle collisions with the obstacles we also have a virtual cinemachine camera set up to follow our character around we created this scene in the earlier videos of our 3d platformer series so take a look if you want to know how it was done for the third person camera we're going to use a cinemachine free look camera cinemachine is an optional package that can be installed from the package manager it provides a suite of tools that allows us to easily create different camera behaviors without writing any code let's delete the existing virtual camera then we'll create a new free look camera from this in machine menu we'll drag our character into the follow field we'll also drag it into the lookout field the cinemachine camera will now update the position and rotation of the main camera so that it follows the character and always faces towards it in the scene view you'll notice these three red circles around the character these show the three different orbit radiuses of the camera we can change the height and radius of the top middle and bottom rigs to change how close the camera is to the character as the height of the camera changes we'll have a larger radius when the camera is high and then zoom in as the camera gets lower we'll set the top rig to have a height of 3 and a radius of 9. we'll set the middle rig to have a height of 2 and a radius of 7 and we'll set the lower rig to have a height of 1 and a radius of 3. let's press play to try this out now we can move the mouse left and right to orbit around the character if we move the mouse forwards it will raise the camera and zoom out if we move the mouse backwards it will lower the camera and zoom in at the moment you'll notice that the camera is focusing on the character's feet let's stop the game to change this we'll add a child object to the character for the camera to focus on we'll set the position to 0.8 on the y-axis then we'll assign this as the object for the camera to follow and look at let's press play to see how this is looking now now the focus of the camera is much higher on the character currently when we move the character it doesn't take the rotation of the camera into account we need to change the movement script so that the character moves in the direction the camera is facing let's recap the relevant parts of this script quickly we're getting the horizontal and vertical axis input we're then using these values to determine the direction of movement along the x and z axis we get the input magnitude and we multiply this by the maximum speed to get the speed we then have some code to make the character jump which isn't really relevant to this video we use the direction and speed to calculate the velocity which we then use to move the character if any of this seems unfamiliar have a look at the earlier videos in the series where this is covered in more detail also you can find a link to the full script in the description okay to make the character move in the direction the camera is facing we need to rotate the movement direction by the camera rotation to do this we first need a new field for the transform of the camera we'll mark this field as serializable so that we can set it from the inspector then in the update method we'll modify the movement direction we only want to change it based on the rotation of the camera around the y axis to do this we'll create a new rotation with just the y rotation of the camera using the quaternion angle axis method for the angle we're passing the y angle of the camera rotation which we can get via the euler angles property of the camera rotation we'll set the axis of rotation to the y axis using the vector 3 up shorthand we'll then multiply this with our movement direction this will apply the y rotation of the camera to the movement direction while we're in the script we'll also hide the mouse cursor so we don't see it move around as we rotate the camera as the cursor state can be changed by the operating system or unity we'll hide the cursor when the application gets focus we can do this by implementing the on application focus method we'll check whether the focus parameter is set to true if it is it means our application has focus and we can set the cursor lock state to locked this will hide and lock the cursor to the center of the view we'll add an else statement and set the lock state to none if the application doesn't have focus let's save this script and switch back to unity we need to drag the main camera to the camera transform field we drag the main camera rather than the free look camera as it's the main camera that actually moves and rotates the cinemachine free look camera just controls the main camera let's press play to try this out now if we move the character forwards it moves in the direction the camera is facing another feature we can enable is to have the camera automatically realign to face the same direction as the character we'll stop the game and select the free look camera in the hierarchy we'll change the orbit binding mode to lock to target on a sign this will give us the additional option to recenter to target heading which will enable let's press play again now when we move to the left the camera will try to realign causing the character to run in a circle and when we stop the camera will rotate to face the same direction as the character if we move the camera so that there's an obstacle in the way we can no longer see the character we're going to use another component of cinemachine to stop this from happening click on the extensions dropdown and select cinemachine collider this will check if the view to our target is blocked and will try to automatically resolve this we can select from various strategies preserve camera height will attempt to change the view while staying at the original height pull camera forward moves the camera along the z-axis preserved camera distance changes the view while trying to maintain the original distance from the target we'll select pull camera forward let's press play to try this out now when an obstacle gets in the way the camera moves forward to keep the character in view and when the view is no longer blocked the camera returns to its original position at the moment the camera moves instantly between the different views we can improve this by adding some damping to the movement let's stop the game and set the damping value to 2. this determines how quickly the camera will return to its normal position we'll also set the damping when occluded value to 2. this determines how quickly the camera will move when trying to resolve an occlusion let's press play to try this out now when the view is blocked the camera moves smoothly to keep the character in view and when the view is no longer blocked the camera returns smoothly to the original position the final change we're going to make is to allow the camera to be controlled by a gamepad we'll go to edit project settings we'll select input manager what we want to do is configure the right thumbstick of a gamepad to act like the mouse to do this we'll right click on the mouse x setting and duplicate it we'll change the dead value to 0.1 and the sensitivity value to 0.5 this will make the camera respond properly to the thumbstick without being too sensitive we'll set the type to joystick axis and choose the fourth axis this is telling unity to treat the right thumbstick moving left and right the same as the mouse moving left and right we'll then do the same with the mouse y setting this time we'll choose the fifth axis let's press play to try this out now we can control the character and the camera with a gamepad you may find that the camera turns the opposite way to what you'd expect if it does you can invert the direction by deselecting the invert checkbox on the axis now the camera will orbit in the opposite direction ok that covers everything for this video hope you found it useful please leave any questions or feedback in the comments and subscribe and click the bell icon so you don't miss the next one if you find our channel useful and would like to help support our work you can find us on patreon and coffee.com thanks guys
Info
Channel: Ketra Games
Views: 96,209
Rating: undefined out of 5
Keywords: Third Person Movement Unity, Third Person Camera Unity, Third Person Camera, Third Person Movement, Camera Occlusion Unity, Unity Cinemachine, Cinemachine, Unity, Unity Tutorial, unity3d, development, programming, coding, C#, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D, Unity 3d, Unity Tutorials, unity 3d Tutorial, Tutorial, Tutorials, Game Development Unity, Unity Game Tutorial, Unity Tutorial for Beginners, Unity Beginner Tutorial
Id: jiyOZbKRfaY
Channel Id: undefined
Length: 10min 50sec (650 seconds)
Published: Sun Jul 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.