THIRD PERSON MOVEMENT in 11 MINUTES - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so after coding a first person controller and a lot of movement abilities for it i'm sure some of you want to know how this would look in third person so today i'm going to show you how to create full third person movement how to set up different camera behaviors and on top of that i'm also going to explain how you can easily use my other tutorials in third person so let's get started inside of your unity scene create an empty game object call it player and add a rigidbody and make sure it's set to continuous and interpolate then as children of these objects create a capsule called player object and an empty game object called orientation and this is basically the setup so if you want to you can of course replace the capsule with a real 3d model but for the sake of this tutorial i'm just going to add a few glasses and that's it so now the first thing that we're going to implement is the camera and since coding third-person cameras is quite tricky we're just going to use an awesome package called cinemachine so go to window package manager and then under my assets search for cinemachine then you can just hit download and import and you're ready to go and thanks to cinemachine setting up the camera is going to be really easy just click on cinemachine and then create free look camera this will create a new object with a free look component on it and basically how this works is that you can change all of the settings here and then the component will control the movement of your camera so drag the player into the follow and look at slot and if you now hit play it's already somewhat working but the settings are quite weird now you can of course go through all of the settings yourself and adjust them however you like but as a starting point let me just give you the settings that i'm going to use so first i like to turn the fov up to 50 and then under access control where you can change the input settings just flip the invert axis bools the other way around now you can see these three red circles around the player those are the camera orbits basically they define how far the camera is away from the player at different heights i'm going to use these values but this is really personal preference so make sure to experiment with it and in the rig settings i'm just going to turn off all that zones so the camera follows the player more precisely you can also make the camera aim a little bit higher while being in the middle rig and if you now hit play you have a great setup for third person camera and of course you still need to decide what style you're going for but i'll explain that in the end of the video because now we're going to code the rotation of the player so there's two things to do here first we want to rotate the orientation of the player to look in the direction where the camera is looking this way we can store what forward means for the player and secondly we want to rotate the player object in the direction of the input so from the camera perspective it should face to the right if the player presses d so open up a new script call it something like third person cam and let's start by defining a few variables you want to access the transform of the orientation the player as well as the player object and also get a reference to your rigidbody and last but not least create a float for the rotation speed now in void update you want to calculate the direction from the camera to the player to find out where forward is also we're going to ignore the y-axis so use player.position minus transform.position without the y-axis and then you can just set orientation.forward to this direction now to calculate the input direction we first need to get the horizontal and vertical keyboard inputs and then use orientation.forward times the vertical input and orientation.write times the horizontal input now if the input direction is not zero we want to smoothly change the forward direction of the player object to the input direction using our rotation speed and if you now go back to unity assign all of the variables and hit play then all of the rotations should be working now let's get to the movement we're going to use the same code as in the first person movement tutorial and i'm assuming you've already seen it but if not no worries you can just head over to the tutorial skip to this timestamp and follow along because as said the code is going to be exactly the one we're going to use for this controller okay so you either just watch the tutorial or you've seen it before anyway you can now put the movement script on your player and set the variables to something like this and if you hit play you can see that the player is always moving relative to where you're looking and rotating to where you're moving now there's just one problem left you might have noticed that sometimes the camera just flies through objects and you can't even see the player anymore of course we need to fix that and cinemachine provides an easy way to do so inside of the cinemachine freelook component just add a cinemachine collider extension then you can select the layer that the camera should avoid for me all of the objects here are in what is ground so i'll select that and also ignore the player tag and make sure that the player object is using this tag then you can just set the strategy to pull camera forward and hit play and with that you just created a full third person controller but you still need to decide what kind of camera behavior you want so let me show you how to achieve those three different styles the first one is the one we just created forward is always where the camera is looking and the player rotates towards the input this style is great for exploration games where the main focus is not on combat if you want to make a third person shooter however this style works a lot better the player is slightly on the left which means you can put a crosshair in the middle of the screen and the player is always facing in the forward direction you can also switch between those two styles depending on whether the player is walking around or fighting to implement all of this i recommend creating a camera style enum like this and also add a transform called combat look at then you only apply the rotation code we created before if the basic mode is active now if the combat mode is active instead of looking at the player you want to use combat look at which is an empty game object inside of the orientation and it's placed somewhere here so calculate the direction just as we did before and the player direction is a bit more tricky but this code should work just fine and now of course you also need to change the cinemachine settings for this just duplicate the old third-person camera then you can call the new one combat cam and change the settings use the player as follow object and the combat look at as look at target change the fov to 45 and set the orbits to something like this and also i'm going to remove all of the dampening effects but you can leave them on if you'd like to and with that you can achieve a great perspective for third person combat games then the third style is kind of like a top down view rotation wise this style is the same as the basic one but with difference in the machine settings generally this view is great for games where you need to see a bigger part of the map at once this can be the case in puzzle games or in top down combat games where strategy and positioning plays an important role so go back to the script add a new style and then use the basic rotation code if this style is active then duplicate the third person cam object and just change the orbits to something like this and there you go you're now ready to code the next league of legends now let me also show you how you can smoothly switch between those styles just add the free cinemachine objects to your script make a function called switch camera style and in there first deactivate all objects then activate the correct one and set the current style to the new one then you can use a few simple get key down statements to switch to different styles using the number keys if you now go back to unity assign the variables and hit play you can change the cinemachine objects and the camera will then smoothly transition from one style to a different one and this is actually really cool as soon as a player equips a gun for example you could quickly switch to the combat mode when he unequips it again you can switch back to the basic view and if there's a puzzle sequence in your game you could even switch to a top down view okay and now if you want to use my other tutorials to improve this first person controller here's how you do it follow along with the tutorial as you would normally and then the only thing you need to do is whenever i talk about rotations or directions quickly check if this applies to third person for example when i say use a raycast in the cam.forward direction to check if there's a wall in front of the player then in third person this doesn't make any sense because the camera is not looking at the wall it's looking at the player so you would need to use orientation.forward instead but apart from rotation stuff everything stays the same now as always if you have trouble setting anything up just head over to my discord server and there you can download this entire project file for free but now thank you so much for watching if this tutorial has helped you in any way make sure to like the video in return and subscribe for more awesome tutorials see you next time
Info
Channel: Dave / GameDevelopment
Views: 186,815
Rating: undefined out of 5
Keywords: third, person, movement, unity, tutorial, third person movement, third person, unity movement, fps, character controller, unity character controller, unity third person movement, fps controller, unity fps movement, rigidbody movement unity, rigidbody, rigidbody movement, unity fps, unity movement tutorial, game development, unity tutorial, how to, fast, basic movement unity, unity third person movement in 11 minutes, game dev, unity character controller tutorial, easy, beginner, dev
Id: UCwwn2q4Vys
Channel Id: undefined
Length: 11min 7sec (667 seconds)
Published: Sun Jun 12 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.