Create a Third-Person Camera System in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this third part we are going to work on the camera movement so we'll create this third person camera that we can use to look around with a mouse we have the Collision which is a cool feature for a third person game and as you can see the player is moving to the Direction Where We are looking and before we get started make sure to hit the Subscribe button that helps me a lot and let's jump right into it so to implement a third person camera for our game I'm gonna use the cinemachine package that allows you to create any kind of camera movement to do that we have to go to window then package manager make sure that Unity registry is selected and search for cinemachine I've already installed this version 2.8.9 using the install button and once you do that you will be able to create a virtual camera under the hierarchy let's right click cine machine and we have virtual camera option you notice that the camera moves to the same position of this object and that's because it's controlling our main camera so let's drag in our player holder to this photo parameter we can change some other settings like the field of view let's use 50. we have the follow offset Vector we can change the offset between the player and our camera if you are making a third person shooter you need to change the X offset so that you can see the weapons for me I'm going to use 0 and let's hit play and there you go the camera is following our character for now I'm using the transpose option as the body let's try a third person follow and this will give you more options the same thing we have the offset we can adjust it as well as the camera distance so let's use 15. or maybe 12. I'm gonna move it a little bit to the Top by changing the Y offset for the X I'm going to use 0 because I want it to be in the middle I highly recommend you to play with these settings and there you go the camera is following the player if I rotate the player to the right side using the D key the camera is rotated to the same orientation it seems like the camera is in the same position let me bring this in view over here so that you can check to solve this problem I'm not going to use the player holder as the follow parameter I will create an empty game object under our player using right click create empty and let's call that camera follow Target then instead of using the player I will drag in this object now we can easily reference this camera follow Target and set its rotation so that we can rotate our camera accordingly to implement that I'm gonna go under the robot controller script and use serialize field then transform camera follow Target after that I'm going to create a function to control the camera rotation using void camera rotation we simply have to access the camera for the Target dot rotation for now I will set it to a zero rotation using quaternion dot identity I haven't called this function yet you could add it under the update function but I recommend you to use delete update so it will add a little bit of a smoothness because it's called after the update function and let's write the name camera rotation we have to select the player holder and reference the camera follow Target and let's hit play and there you go the camera is following the robot and it doesn't affect the camera rotation because we are setting the camera for the Target rotation our next step is to take the inputs from the mouse in our first video we have added this controls input actions like the move action which we have used to move our character now we'll use the look action if you haven't watched that video make sure to check it out the same thing let's use the player manager input script and add a function or look which takes in a variable of type input value let's call it value so this function is called each time we move the mouse left and right or forward and backward in such case we will take that variation which is a vector 2. let's create another public variable Vector 2 I'm gonna call it look under the Outlook function let's use look equals value then dot get vector2 now we can take these inputs and use them under the robot controller first I will add two variables for the X and the Y rotations using float and let's call it X rotation and another float for the Y rotation I've already created a variable for the player inputs manager that has the look vector now we can move under the camera rotation and we can use x rotation plus equals basically we can add the Y value of the loop Vector using input dot look dot y and we're going to rotate the camera around the y-axis so that we can look left and right using the mouse X variation using plus equals input dot look dot X finally we can create a quaternion based on these values I'm gonna call it rotation equals quaternion dot error so that we can pass in the angles for the X I'm going to use the X rotation for the Y let's use the Y rotation but for the Z I'm gonna pass in 0 and finally we can assign this rotation to the Target so that we can rotate the main camera let's save our script and let's hit play now we can rotate the camera and look around to fix this problem I'm going to clamp the value of the X rotation using X rotation equals and let's use math F dot clamp which takes the value that we want to clamp which is the X rotation and the minimum and the maximum value for example I wanted to be between minus 30 and 70 degrees I recommend you to add two variables for these on top let's save the script again and check the result the X rotation is limited to be between -30 degrees and 70 degrees so if the camera hits the ground we can't see it because we are not checking for collision between the camera and the ground to fix that we can select the virtual camera luckily we have the option to check for the camera Collision for now it's not checking for any objects we can select our ground and assign a specific layer to width using add layer I'm gonna use ground well let's select it again and change its layer from default to ground last but not least we can select the virtual camera and we need to tell it to check for collision between the camera and the objects that has the ground layer by selecting ground if the camera hits the ground you see that we have this kind of movement and that's what we want and the final thing that we have to fix about this camera movement for now if I hit the z-axis to move forward the player is still moving to the same direction which is the forward vector and that's because we have created the target direction using the move Vector we have to take into consideration the camera y rotation so let's rename this Vector to be the input Direction that is created using the move X and Y values the same thing I'm going to create a rotation based on this Vector input Direction but we're going to add the camera y rotation to this value of course we have to add a reference to width on top using serialize field and let's declare it as a game object and call it main cam but we can't add the Y rotation to a quaternion that has X Y values that's why I'm going to change this variable to be a float which is the target rotation equals quaternion.look rotation dot input Direction and to get the Y rotation we use dot error angles dot y then we can add the main camera while rotation using main cam dot transform dot rotation the same thing dot other angles dot y so we have this error because we have used the target rotation in the previous video as a quaternion so that we can change the player rotation we can create it again using quaternion and call it rotation equals then let's add quaternion dot error for the X I will use 0 then I'm going to use the target rotation which is a float and it's the Y rotation that we need and finally we can use this new quaternion to rotate our player now that we have the target rotation that we need and it is created using the inputs as well as the Y rotation of the camera we need to get the direction based on this rotation so let's create a vector 3 and call it Target Direction and the expression is a little bit weird basically we will take the vector3. forward and to rotate it using the target rotation we have to multiply it by the equatorian Target rotation and I think that's pretty much it but we don't need to create the target rotation if we are not moving so I will take this line of code and I'm going to set it to zero then I will assign the value under this if statement that means when we are moving we need to calculate the target rotation first make sure to reference the main camera object and let's hit play for now we can rotate the camera so that we can look around and let's try to move forward the plate is moving forward so fast because I have made a mistake here we are multiplying the forward vector by a float which is the target rotation actually we have to set it to a quaternion so that we can rotate the vector dot error and percent 0 for the X then the target rotation and 0 for the Z let's save it again and it doesn't work the player starts moving without pressing the AWS SD keys don't worry everything is fine actually we have used the speed variable and it is initialized to 4 by default I'm gonna rename this variable to be move speed and I'm gonna add another float on top I'm gonna call it speed by default it is zero and once we start moving I'm Gonna Change it to the move speed which is 4. and there you go we have finished this third person camera movement now we can look around with the mouse and the player is moving to the Direction Where We are looking I think that's pretty much it guys for this video I hope you like it in the next video we are going to add a special transformation we will be able to turn this character into a ball that's why don't forget to subscribe to my channel and hit the Bell icon so that you get notified with my videos and they will see you in the next one
Info
Channel: GDTitans
Views: 8,868
Rating: undefined out of 5
Keywords: GDTitans, unity, unity 3d, unity third person camera, third person camera, create third person controller unity, unity third person, unity cinemachine, unity 3rd person, third person controller, unity3d, beginner, easy, how, to, howto, learn, tutorial, basics, camera, controller, movement, player, 3d, cinemachine, character, easy character controller unity, Unity Cinemachine, Unity camera tutorial, Third-person game, Camera controls, Unity game development
Id: 04EpnVbMKpU
Channel Id: undefined
Length: 11min 34sec (694 seconds)
Published: Mon Jan 23 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.