Make a Third Person Shooter with Cinemachine and the Unity Input system

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when it comes to making games there's some differences to consider between third person and first person games specifically how you control the camera for a first person game you can just lock the camera directly to the player and be done with it but a third person game usually you want to be able to rotate around the player and you might want to have the view set up to look over the shoulder in the case of a game like this so in this video i'm going to cover how to set up the camera for an over-the-shoulder third person game as well as how to control the character using cinemachine and the input system let's start from a brand new scene the first thing i'm going to do is set up some of the environment components so a ground plane and just some random cubes now let's create the character i find that it works best if you create an empty parent object to hold all the parts of the character let's add a capsule to act as the model for the character and make sure you move it up by one so it's not halfway in the ground and then under the parent object we'll add a character controller component we also need to make sure we move the collider of the character controller up by one so it lines up with the model let's also add in a cube to the player just as an indication of which side's the front if you don't have cinemachine and the input system installed go up to window and then package manager then make sure you have unity registry selected search for each of them and hit the install button in the bottom right now that we have a character let's get the camera set up we'll do this by going up to the cinemachine window and selecting create free look camera this will put a new free look camera in the scene so let's select that and the first thing we're going to want to set are these follow and look at targets to do that let's add a new empty object to the character and call it the camera target now we can position that target to be over the character's shoulder now back under the free look camera we'll drag this target into both the follow and the lookout fields we can ignore most of the settings on the free look camera for now but let's really quickly change the field of view which is located under the lens tab now the settings we're most interested in are under this orbit section down here starting with the middle orbit we want to set the height to zero so that it's on the same plane as the target it's looking at for the bottom orbit we want to set the height to negative one so it's right on the ground and then for the top orbit you can set it to whatever you want to the radius value adjusts how far the camera is from the player you can adjust this to get the look that you want under the x-axis we're going to want to change this value field until we get it behind the player if you want to see what the view is like from the different orbits under the y axis you can change the value between 0 and 1 where 0 will put you on the bottom orbit 0.5 is the middle orbit and 1 will be the top orbit i'm going to play around with the orbit settings a little more until i get something that i like keep in mind that the top orbit will be the furthest you can look down and the bottom orbit is going to be the furthest you can look up you can also change the spline curvature setting if you want to adjust the shape of the path between the orbits if we look under the axis control section we can see that each of the axes has an input axis assigned to it this is where you tell unity how you want to control the camera but since we're using the input system this is just going to throw errors at us because it's trying to use the old input manager what we need to do to fix this is add a component to the free look camera which is called a cinemachine input provider what this component does is it overrides those input axes and insteads lets us pass in an input action we don't actually have any actions yet so let's create an input action asset and start making some the first thing we need to do in the input action asset is create an action map and then we can start defining actions the first action we make is going to be the look action which lets us control the camera now we want to change the action type from button to value and then set the control type as a vector 2. and then we can set the binding as the mouse delta which is the difference in the mouse position between two frames when you're working with the input action asset make sure you either check the auto save box or don't forget to hit the save asset button now if we go back to our free look camera and scroll to the bottom under the input provider for the x y axis we can select our player look action now that we have the input provider component if you went back up by the axis controls you'd notice that under both the axes the input axis is missing which means that it's being overwritten by this input action but that means now we should be able to hit play and move the camera around now you can see that moving the mouse up and down moves the camera between the orbits and then moving the mouse back and forth moves it around whatever orbit it's on that's all we need to do for the camera controls so let's add in the rest of the actions and work on getting the character moving let's first create the move action we'll set the action type to value and change the control type to vector 2 and then we'll add a 2d vector composite to set up the movement keys then let's also create a fire action that i'm going to map to the mouse button now we need to create a script to handle character controls and movement one of the first things we want in the script is a reference to the camera so i'm going to add in a serialized field for that my auto complete doesn't seem to be working so i guess i have to type the whole thing out since this script is going to handle the movement we're also going to want to be able to set a movement speed then we'll also need a reference to the character controller that we have on the player and we'll need to set a value for gravity in the start method we'll get our reference to our character controller using the git component method then on each frame we want to tell the character to move so let's call a move function and then go create the move function but before we can tell the character how to move we need to read the input so let's get a move input function started for this function we're going to need to include the input system and then pass in a callback context as the parameter we also need to save the move input so we can use it in the move function so i'll create a new vector2 at the top and we'll save the value directly into that now we can use context.readvalue to get our move input now in the move function we want to create a velocity vector since we want the player to move relative to the direction the camera is facing we need to take the right direction from the camera and multiply that by our x component of the move input and we need to take the camera's forward direction and multiply that by the y component of our input we also need to add in the gravity so we're going to multiply vector3 dot down by gravity and then we'll multiply this whole thing by time dot delta time and then by the move speed you usually wouldn't want to multiply the gravity component by your move speed but in this case it doesn't really matter too much now we want to call the move function on our character controller so we'll say controller.move and pass in our move velocity vector now we should be able to move our character around except it looks like i have a few errors so let's go fix those it looks like my error highlighting isn't working either okay now i fixed my error highlighting so let's go ahead and fix all these problems and then it should work well it would work if i didn't forget a bunch of stuff first we need to add in a player input component then we can drag in our input action asset switch the behavior to invoke unity events and then set up our events except i also forgot to add our script to the player now back under the player input we want to select our control script and use the get move input function and now we get another error because i forgot to assign the camera but hopefully this should work now and that looks like it's working now the next thing i want to do is make the player face the direction that they're moving so let's head back into our script and add a rotate function for this function i'm going to pass in a target position to rotate towards so to look in the direction we're moving we want to take our current position and add the target which was going to be the direction we're moving in and tell our character to look at it now in the move function we can call rotate and pass in our move velocity except we want to set the y component to zero if we don't do that then the player is always going to be staring at the ground now if we test that we can see that the player will face whatever direction we're moving in and it still gives us the option to rotate the camera around and look at the player now the next thing i want to do is set up the fire action but first let's add in a reticle so we'll create a canvas and then drag over this sprite to act as a reticle with that we can head back into the script and let's create an onfire function this is another function we're going to tie to the player input so again we want to pass in a callback context for this function we first want to make sure the action was performed so we're going to say ifcontext.performed and then we'll put all the logic inside that in this case i'm going to use a raycast to act as a projectile so first thing i want to do is get the center of the screen so i'll create a new vector2 and set it to the screen width cut in half and then the screen height cut in half then we're also going to want to create a raycast hit which will be the object that we hit with the array and then we'll create the array by setting cam.screenpoint array and pass in our center position now just to show that it's working we can use debug.drawarray and we want to pass in the origin of it and then we'll say the direction and we'll multiply it by 100 because that defines the end point i'll set the color to blue and then make it last for two seconds now to detect if the ray hits something we want to say physics.raycast and then pass in our array and then as an output parameter we want to pass in the raycast hit and for now we'll just log the name of whatever item we hit now let's go see how that works oh i seem to have another error though let me check that out first so another error saying we're using the old input manager and that probably comes from the event system when we created a canvas it adds in an event system by default the event system uses the input manager so we need to select that and tell it to replace it with the input system module i also forgot we need to subscribe the on-fire method to the fire action in our player input component now we should be able to test it and we can see that every time i click we cast a ray from the center of the camera straight out and it'll log whatever we hit at the bottom one thing that we might want to change though is that when we fire the player doesn't rotate to face the direction that they're shooting in so let's hop back into the script and fix that really quick so when we fire we want to determine our forward direction so let's create a forward vector and we can set that equal to the camera transform's forward direction then we'll also want to set the y component to zero and i guess we can normalize it it shouldn't make too much difference but then we'll call rotate and pass in our forward direction as our target now when we go test it we can see every time we fire the player snaps to the direction of the camera there's one more thing you might want to fix with the camera so to demonstrate let me add a new cube in really quick closer to our player you can see that if i get close enough to this cube and then rotate the camera it'll just go straight through the cube and we can see inside the cube to fix that we just select our free look camera and you scroll down to the bottom and you'll see add extension so we want to select that and then pick cinemachine collider and that will handle all the collisions for the camera for you so you can no longer put the camera inside objects the last one i want to show you is how you can add a gun to the character first i'm going to add a new empty object to our character and i'm going to call it the gun parent and i'm doing this so that i can define the origin of the gun so i can rotate it around a different position i'll go ahead and move that up to about where i think the gun should be and then i can add in a new cube underneath the parent now you can see if i select the parent the origin of the gun is further toward the back so when i rotate it it'll rotate from that point instead now if we hop back into our player controller script we can add in some rotation for the gun so that it points up and down with the camera the first thing i'll need to do that is another serialized field to get access to the gun and we'll actually want to pass in the gun parent i think i'll handle the gun rotation under our rotate function we've already created so we're going to want to take the gun transform local rotation and let's try and just set it equal to the rotation of the camera so now if i hit play the problem with this is i don't want the gun to rotate away from the player like it does or be able to rotate into the player i just want it to point up and down so to do that we need to do a few different conversions between euler angles and quaternions so i'll say quaternion dot euler then we want to pass in a new vector 3 and create a rotation vector from the camera's rotation since we only want to rotate around the x axis we'll set the other two components of the vector to zero that doesn't work quite right so let's go look at what i did wrong it might be that we just want the rotation from the camera instead let's try that and that seems to be about the same i know what i did wrong i'm taking the quaternion from the camera's rotation and trying to use the x component of that when we want to convert it to an euler angle first now it should work right except i'm gonna go adjust the sensitivity a little bit on the x-axis it's really fast i also just realized that the camera controls are inverted so if you want to change that you can go hit the invert check box on each of the axes there we go that's everything working now so hopefully this was enough to get you started in making a third person shooter and if you liked the video be sure to leave a like make sure you're subscribed and thanks for watching i'll see you next time you
Info
Channel: Soda Rocket Studios
Views: 902
Rating: undefined out of 5
Keywords: Unity, Game Dev, Indie Game dev, indie games, unity input system, unity input system 2021, cinemachine unity, cinemachine tutorial, third person unity tutorial, third person unity controller, third person shooter unity, unity tutorial
Id: dqfVlSxOXv8
Channel Id: undefined
Length: 20min 51sec (1251 seconds)
Published: Fri Aug 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.