HowTo: Build a 3rd person shooter in Unity - Part 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back Jason here with unity3d College today we're going to continue on the third-person shooter series and we're gonna add in some mouse controls now where we left off I'm gonna hit play we could run around and shoot we can add grow a character and even kill him right so if I get up close you should start coming in and if I click enough times enough shots get fired off in he dies so let's get this set up what we're gonna do is change out our V cam just a little bit and then add a custom camera controller script to it before we start coding though I want to change our look at Target right now we're looking at the head so if we click right here you'll see that we're looking at the character's head that's where that red line is going to when once we actually start playing and we want to change this so that it's always looking at the player instead to do that I'm going to take the player and just drop that right into the look at Target and then I'm gonna adjust the offset right here this tracked object offset Y value I'm gonna go to the game view though and just grab the Y and drag it up until it's right about the height I want I want to kind of just over the player shoulder so I'm gonna get it just over their head actually just a little and then I'm gonna adjust the follow offset to one and the tracked object offset to one it's that way I'm right over in this shoulder area of course you can pull this in or out just in however you like but this should just seemed like a good spot to use now when we hit play I want you to see what happens when I adjust the Y value here so I'm just gonna grab the Y drag it up and you see we look up and if I drag it now and you see we look down so there we go we've got nice easy up-and-down movement and remember we can still turn with the keyboard so let's stop playing and let's create our camera controller script so I'm in the scripts folder will create a c-sharp script name it camera controller and we'll open that up and I'm gonna get rid of these comments I don't need them and clean up my formatting with ctrl K ctrl D then I'll mark start private explicitly again it doesn't make a difference but I like to do it just because it feels a little cleaner to me and then we need to get a reference to the value that we want to change and that's this aim right here so we're gonna want to change that y-value of the aim up claps down automatically after a bill okay no it doesn't want to expand whatever we want to change that value so to get the object that we need to adjust we're it's the composer we're gonna say composer equals and then we need to get component and we need to get the virtual camera first so I'm going to send a machine and just type in virtual or vir there we go we want this virtual camera right here and then we need to get a cinema sheen component from that virtual camera so to get Cinna machine component and here we'll do send a machine dot and I want the composer so just start typing compose and that's the one add the parentheses and semicolon now I hit control period and generate that field and when I generated the field since cinema Sheen composers in the cinema scene namespace the using statement was augment already automatically added so I can just delete these right here and clean that up just a little bit so now we're getting the composer in start and we just need to update it in update again I'm gonna explicitly mark update as private doesn't make a difference but just something I like to do and then we need to read our mouse input so I'm going to say blow vertical equals input get access and we're going to use mouse Y and I want to multiply this by a sensitivity value so that we can adjust it at runtime so I'm going to say times sensitivity and then we'll generate a field for that add the serialized field attribute so it shows up in the inspector and give it a nice default value of 1 I'm also going to move this up above like that my serialize fields right at the top just keep it nice and standardized so now what we can do is say composer Y or no not not Y dot M look let's know it's a track the object offset get that mixed up with him so we need to get the tracked object offset and set the Y value of that and we're not actually going to set it we're gonna say y plus equals vertical so this all allow us to just adjust that offset by moving the mouse up and down now I'm going to go back over here and hit play and we'll show you one issue that we're gonna run into with this that we need to adjust with one more line of code I haven't actually added the script so let's add that now at runtime I just got to remember to add it again yup camera controller is added and now if I move the mouse up I look up if I move it down I look down but I want you to look at the track the object offset Y value right there if I move it up and I just keep moving up see the value just keeps going higher and higher now I'm back on the screen and I'm dragging down and you can't see it but I'm actually having to drag down a whole bunch just to get it back up and it's not really locked in so we can fix this with a simple clamp statement but before we do I'm going to add the camera controller to this camera so I don't forget when we jump back in there we go it's add it and we'll just double click it go back in here and we're gonna say composer dot M tract object offset dot y equals math F dot clamp and then we give it the original value so here let's just copy that and then we can give it a minimum and a maximum value and this is just gonna make it so that the value is either the current value or the min or the max depending on if we go below with the minimum or above the maximum so if we have you know let's put a negative 10 and a positive 10 for these two values and then the far Y ever gets to greater than 10 it's gonna clamp at 10 if it gets lower than negative 10 it'll clamp at negative 10 so I'm gonna save that remove those using statements and that extra space at the end jump back over and hit play and let's see if we can look up and down with it nice and clamped and again if you want to clamp this even further you could so that the player can't look all the way down you can figure out like I mean really is that about a value of 4 or 3 right there oh that's a 5 so value of 5 would be you're looking up there 10 just goes all the way up and all the way down relatively smooth okay so we're good with up and down and we can again use the keyboard to look left and right but I really want to be able to move my mouse around and aim so we need to add in some rotation based off of the to do that we're gonna make a super simple change we're just going to the player movement script and if you remember here we're getting our horizontal axis and then we're rotating based on that horizontal axis we can actually change this just be Mouse X and save then jump back over here and hit play and now that it's done compiling now I can use the mouse to look around to aim and my mouse cursor still kind of going off the screen so it's a little bit hard but it's generally working it feels pretty smooth pretty quick so let's say I want to get the mouse to not do this stuff though so that I can actually click over here or move the mouse far enough to the left or the right and have it stay centered and you know basically lose the mouse cursor so to do that super super simple we'll just open up the player movement script and we'll put it in here temporarily I'd recommend perhaps adding another script that does this or adding a toggle option for this but it's like it's a one-liner so it's pretty simple will say cursor dot lock state equals and then we have cursor lock mode enum and there are three different options there's confined just locks it into the game window there's locked which is what we're gonna use and that's basically making the mouse cursor disappear and centering it in the middle of the scene that all the time and there's none that's what we're on by default so I'm gonna set it to locked right here and when I do that and hit play now you see that the mouse cursor is just gonna disappear and goes far left or right or whatever I want and still aim and click and not be clicked out of my game there we go and run over here keep shooting in and yeah we're good so if I want to get the mouse out of here by the way just hit alt tab and then you can get it kicked back out but again that's why I recommend putting in a script with the toggle option so you can just lock the mouse in or maybe just to lock it by default and have a hot key that unlocks the mouse and going forward on this series I'm going to continue to do smaller size chunks like this and try to keep it a little bit more regular again if you have requests for a specific function they just drop a comment below I'm also gonna put all of the source available for this online so you can just download it and grab it pull it in I cannot put the art in there though you'll have to grab that yourself and kind of put everything together but I think you can at least check against the source and make sure that there are no problems also if you're interested in more in-depth training I do have a course on this stuff where we go basically from the start to building out a full first person third person shooter style game do a lot of this stuff but in a whole lot more detail many hours of content a day and I'll put a link to that down below too you can check it out if you're interested but anyway thanks for watching don't forget to like and share it really does help and have a great day keep coding
Info
Channel: Jason Weimann
Views: 31,881
Rating: undefined out of 5
Keywords: unity3d shooter, unity shooter, unity3d 3rd person, unity 3rd person shooter, unity third person shooter, unity 3rd person, build fps, unity third person, unity fps game, unity3d 3rd person game tutorial, unity third person camera, unity third person controller, unity3d third person controller tutorial, unity tutorial for beginners 2017, unity tutorial c#, unity tutorial 2018, unity (software), untiy build fps, unity3d third person
Id: zdYx2Kh86TA
Channel Id: undefined
Length: 9min 31sec (571 seconds)
Published: Mon May 07 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.