3rd person shooter in Unity - Part 3 (3rd person controller and more animations)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up Jason here it's time to continue on with our third-person shooter game right now it's not really third-person if you remember that it kind of is the character moves along but it's not really lined up with our character like we're not following around and I want more of a Gears of War style movement where the cameras can following and snapping behind and we'll have a little reticle in the middle where we can shoot and blow things up so let's just start building that out now so I'm gonna stop playing the first thing we have to do is make a relatively big change to the character controller it's a small enough script that it won't take long but we need to change it so that he's no longer moving in set directions based off of what way I pressed but right now if I push forward he'll go forward if I go left he'll go left right he'll go right and it doesn't matter what direction he's facing he always goes the same way in world space so let's go into the script we're gonna delete out a little bit of the old code change things around and make it all work all right so here's the script and I've deleted everything after we generate the movement vector because again we want to change the way that this motion is handled so let's go in now and the first thing I want to do is instead of setting the speed to the movements the movements magnitude we're just going to use the vertical instead so this will be how fast we're going forward you know W will be forward s will be backwards and we're just gonna adjust our speed based on that so we're gonna do something like animator dot animation anime for that set float speed remember this is the name of our variable in the animation controller and it is case-sensitive and we're just going to pass in vertical now instead of movement magnitude now we always want to rotate our character so what I'm going to do instead of using the rotation stuff that you saw before we're going to do transform rotate and we're gonna rotate around vector three dot up so to see vector three dots it's just gonna go on the y-axis and then the amount that we want to rotate is going to be a horizontal value so this is negative one positive one depending on where I'm pushing on the right here or somewhere in between if I get halfway and I'm gonna be like 2.5 then we want to multiply that by a turn speed variable that we already have here and we're gonna multiply that by time.deltatime so that way it's frame rate independent the next thing we want to do is check to see if we should actually move so to do that we'll just go if vertical is not equal to zero and here we can't do a greater than zero because we want to also move on negative values so that way we can allow our character to walk backwards we don't have the backward walking animation in yet but we'll get one in eventually and then this speed will kind of link to it and make them move backwards nice and smooth so to move him all we need is the character controller dot simple move call and we'll pass in transform dot forward now so the direction that we're gonna move in isn't gonna be that movement variable it's gonna be the forward direction so we're turning him and then moving them forward and we're gonna multiply that by movespeed and then we're gonna multiply that by time.deltatime again oh wait no we don't need time at Delta time this is a simple move and we're gonna multiply it by a vertical sorry not time that double time so this reason we do the vertical here is again if it's negative we want them to go backwards if it's forward it's just gonna be one it's not gonna make a difference so I think that's all we needed for that so I'm gonna save it I'm gonna jump back in here hit play and try to walk this guy around a little bit now remember the camera's still not gonna follow him in the third person mode yet so let's see but let's see what it looks like so if I turn right to rotate you can see slowly slowly rotating the reason this is so slow is there since we're changing the way that we're rotating the variable is not really valid anymore at the value of five so let's turn it up to about 150 there we go now you can see him turning at a reasonable speed and again he's not animating yet we can add that in later whoa look at that movement though the movement amount was insane so let's see since oh it's because I pulled out time.deltatime we need to turn this move speed way down so let's go a bit like raise to low maybe five still seems a little low I'm gonna go up to like 725 let's try that backwards again no animation yet forward it seems good so 150 and 7.5 seem like reasonable values for now and we'll adjust it yeah as we keep playing through so let's see this was 7.5 and 150 all right I'm gonna save that and now I want to get the camera actually following this guy around so we've got the motion kind of hooked up but we need it so that the camera stays behind him follows them again thinking more like Gilda war style it Gears of War I said guild war here's a war style movement where you can I have your character there reticle and you can blow stuff up right so let's let's get that set up so to do that I'm gonna go on to my virtual cam remember this is from the sin machine we created a virtual camera in the last video and right now it's set up to look at his head so it's it's aimed at his head if I click on that you see it's going to his head but what I want to use now is the follow option so for follow I'm gonna drop the actual let's just drop the toon soldier demo base object in there and see the camera is down on under the ground now that's because we're looking at his feet and we're kind of clipping through the ground what we want to do is just adjust the offset of the body here so if you expand out this body area we can grab the Y and just drag it up just a bit so that it's above ground and slightly above the character I also want to pull it in a little bit with the z value so just shrink that down and I think I'll pull down the Y a little bit and we pull that out just a tiny and I think that seems right about where I want him I think now I want to adjust the aim and I'm gonna adjust this so it's a bit over his head because I don't want his head in the center of the screen I want to kind of look at a little area just above the screen and I'm going to see if I can offset this just a little bit so it's kind of above him and to the right it's that way the characters here targeting reticle is right around there that's my goal at least let's see yeah I think I'll leave that a zero okay so let's hit play and see how that looks and then we'll make a couple more adjustments and get this all kind of lined up right so there we go now I turn and move camera follows the center is still there so right now the center still a little bit like you can see right now I'd become offset and I want to tighten this circle up just a little bit but I think I'm gonna put on a targeting reticle first and then go in and adjust that so we can really see what its gonna look like so let's go find the targeting reticle and get that imported real quick alright so I think I'm gonna use this one right here that I found online it's painted it white and I'm gonna drop this into an art folder so to create a new folder name an art and I probably won't make subfolders under here right now I don't have enough stuff to need it but eventually maybe if this grows will subfolder it out and have a UI section or something for now though I just need to select this target texture and change it to a sprite and hit apply one thing that I do want to do though is a scale this way down right now it's at 2048 by 2047 we're never gonna be anywhere near that size and having it import as a giant scale and then scaling it down in game is a good way to get nice Jaggi edges that we don't want so much easier and better to just set a max size that's right around the size that I'm gonna use know slightly bigger the next size up and then just use that so I've imported it at 128 and I'm gonna go back over to my scene view and add an image so let's go to game object UI and add an image and then we'll assign that target picture right there now if I go to my game view we should see got a little targeting reticle right there in the center of the screen and I might want to scale that down a little I think I'll leave a big for now maybe adjust it later get it to the right size so we've got that there let's play real quick one more time see what it looks like reticle it's really just gonna stay right at the middle of the screen right now there we go and you see it kind of like follows along now we get a little bit of a delay just kind of what I want but I want to tighten that up just a little bit more and I'm gonna hook up the controller go through it with the controller too so there we go this is a much better way to play so playing with the controller and see I can can aim and target stuff now let's let's tighten this movement up just a little bit and you can adjust this however you want in your game if you want to have looser movement where it's slower to get there you can if you want to go tighter you can't do so here I'm just going left and right and I really want what I want is this targeting reticle in this yellow dot kind of line up like the center of the screen where I'm looking I want that to be there so what I'm going to do is grab these bars here in play mode pull them down and I'm really gonna tighten this up like I think I want it super tight I always want that reticle like right there at the center part where I'm aiming and I should probably move that offset just a little bit too let's see which one was it was it the X ya gonna move that up just a tiny bit more just like that and then let's move and I think that's probably good like that and then let's see the tightness on these seams okay so if you want to adjust the how quickly it goes over you can just slide down and up these horizontal and vertical damping if I slide it all the way to zero you see that now I'm instantly tracked there's no delay at all that's too fast for me point five is a little too slow I think maybe like a point two so it's relatively snappy but not instant right we still get a little bit of that the way and we're not really having an up-and-down issue because I'm not allowing the player to aim up and down right now everything's just kind of forward we would probably add in some up down looking stuff later so then we can adjust those values anyway we've got that set up I'm gonna copy my virtual cam while I'm running so don't stop playing copy that so I've got all the values that I want stop playing right click and paste the component values and that did not seem to work right interesting let's set that horizontal damping back to two I don't know why that went 25 and then let's just pull these back in just like that nice and tight and I'm gonna save my scene all right now I want to hook up the firing so that it's actually on that reticle so that where I'm shooting is based on the reticle not on the firing position of the gun or the direction the characters facing so to do that I'm gonna open up the gun script again and it's gonna be just a minor change what we're gonna do here is instead of using the ray from the fire point position and the fire point forward we'll just get a ray from the main camera so just do camera that main screen point to Ray see - ray actually that seems to be a port point array and we'll just pass in vector 3.1 x 0 point by that and what we're doing here is getting a ray that's right in the center the viewport goes from 0 to 1 on X and 0 to 1 on Y so if we do vector 1 times 0.5 we're gonna get a vector that's 0.5 X 0.5 Y we'll also get a Z but Z doesn't matter here so we're gonna do ray ray equals that and then just get rid of that line and now we have our ray coming from the center of the screen now instead of that fire point and since we did that whole way I think we are spawning the muzzle I'm trying to remember for spotting the muzzle particle there I don't think we were right no so yeah let's delete the fire point we don't need it anymore clean up the code don't leave extra junk in there that you're not using if you're afraid that you're gonna want to go back and look at it later you should be using source control check it in and then you can always go back and look at your stuff later and keep your code clean at the same time so now when I shoot I should see my ray kinda go straight forward into the world it's gonna be a little bit hard to see I think that let's let's pull this up game view and see your side by side and see if we can get a good look at that so here's the character and if I look over here it's it's not gonna be that easy to see the oh we have an error what's the problem let's take a look oh I see what it is so right now we're not actually finding the main camera I think I think the main camera is NOLA which just means that this camera here where's my camera where are you let's go to the camera oh it's under the scene so this camera is not tagged as the main camera so if we set that as main camera there we go error message should disappear and I should be able to shoot okay so I'm able to shoot we're still not seeing the Ray though so let's get that fixed up Oh yep we're not seeing the Ray because debug draw ray was commented out so I'm gonna do is cut that and I'm gonna move it down below this ray here and I'm just gonna replace fire point with Ray in both cases so it's but it's gonna be Ray dot origin and Ray dot direction just like that so now we'll get a Ray kind of coming right out of the camera showing exactly where I'm shooting well once it finishes recompiling there we go there's a little spinner there bring that right in so you can see it spinning as soon as that finishes I should be able to click and shoot there we go now you see the Ray coming off the camera so the Ray is going right to this thing in like if I aim it at this guy and we let see find him right there if I click you should see the lime kind of go right into his feet let's move a little bit closer there we go now it's going right through the center of his right through through a part of his body and actually I think I want to move that Ray up just a tiny bit maybe um yeah we might move that up just a little bit I think it's slightly offset from where the uh the reticle is showing [Music] oh and I see the reason for that right here we have a little bit of a position offset on my image if I just zero that back out now the race should just line up fine so I think since we're already working on this character's movement what I'd like to do next is just get in some more animations for turning and maybe walking backward and to do that I'm gonna jump over to mix omocha they have this nice free giant collection of animations and some characters and once I'm logged in I just need to hit upload character and I'm gonna drop in my toon soldier guy I'm just gonna drag him and drop him right onto here and there we go he should pull up and then I'll be able to apply some animations download those animations and just drop them right into the game and assuming everything goes well it'll be a quick and painless process and this works great for characters that are already humanoids and that are rigged right sometimes they're not and it's a little bit of a pain but a lot of times it works out really really simple so there we go I've got the character in here and I want to find some animations for him I think search for soldier let's see what they've got here so there are probably some packs extras search for soldier pack I think I'm just download a big chunk of animations and use the ones that I care about I mean there isn't a big pack here ok well let's see let's do a soldier turn so I really want a guy with a with the rifle they're doing the term not just um let's see what do we want here maybe the rifle turn so I'm gonna take that one and see what that word I think that would work as a turn let's see what does this one look like she's kind of curious yeah that's not right at all it's good to preview these things and kind of see how it's gonna look turning right 90 degrees so I'm not doing the full 90 degree turn here so I think this this one's probably good and if you look at it when you check the mirror box you'll see that we can also have them turn the other way but when he turns the other way that gun kind of got rotated weird so maybe I'll take a right and a left turn let's see what other terms can we find that's too big of a turn so I know it's a lot it's lots of excitement here Oh actually yeah let's get this sick the pro rifle pack it's got 49 animations in it and we'll just download that now when you hit download you want to make sure that you switch it to FBX for unity and then hit the download button and you'll get I think it's gonna be a nice big zip with 49 animations in it she'll be perfect for what we need here I'll be able to find the ones that I want try them out in game and then you know throw away the ones that I don't care about or just leave them in there because it's not that big a deal so I'm just gonna save this pack into my animation folder for now and then we'll jump back over to unity pull that thing apart and start hooking this guy up so let's hit show and Explorer I need to just extract the zip file where'd it go oh there it is just being a patient all right so let's extract it so I use 7-zip I think you can use whatever you like built-in extraction right in Windows there we go and we've got a big folder full of different animations alright let's try a hook them these up alright I've got my folder here full of these animations and they're not gonna work right off the bat so if I select one and hit play I've dropped my tuned soldier in there you see it doesn't actually play what we need to do is go to the rig tab and we need to change this animation type from generic to humanoid and we also need to set the avatar to match the character that we're using so to do this since we have 49 of them I'm going to select all with control a and then switch the animation type of all of them at once make sure this is on copy from other avatar search for an avatar and I'm gonna search for my soldier that guy right there to soldier demo avatar and then hit apply you know this will just update all 49 of these and I won't have to go in and manually do them one at a time always batch select and batch update if you can all right so now if I select one of these go to animation tab and hit play should see the animation playing and kind of working on the character that is perfect so let's go into our animator now and let's see we have a walk backward I assume we do let's see walking walk backward right there let's see let's hit play on that okay perfect so let's add that in first that's a nice simple one to drop in so I've gone into our blends tree remember if you go to the base layer we just have a motion blend tree I'm gonna double click on that and then select the blend of tree right here and we need to add one more motion so in add another motion field and the motion for this is gonna be that walk backward so expand this out drop the animation right onto there right now it's named mix mo I'm gonna rename this so I'll select this let's just name this walk backward I just want good names in there so I can see what everything is and have it be nice and clean all right so we've got walk backward in here the threshold for walk backward right now is one that is definitely not what we want we want we want this to be negative one right we want what we want to do is change this over so this goes up here and then if our if our walk value or our input our speed is negative we want to use walk backward if it's zero we want to kind of sit at this idle and if it's one we want to walk forward so what we want to do is uncheck this automate threshold checkbox then I can just put in the numbers so just go negative one zero and one so now if we play the animation here right so hit play as I adjust the speed you'll see like when I go forward he's running forward you know as I go positive and if I go negative he starts to walk backward he walks backward a little fast I might slow that down later but I think we can just leave it fast for now okay so let's try that out and see how it looks um in game play if I didn't miss anything I think it's just gonna work right so we'll be able to walk forward and when you can walk backward is he animating that's kind of hard to see from that view where's uh where's my character that real running forward walking backward okay so he is not animating oh and we have an error oh what is this error in gunfire oh let's let's look at that real quick oh I didn't save the scene when I changed the camera to the main so let me let me do that real quick before I forget again so change that over to main camera save now again this main camera call is actually really slow and we shouldn't really be doing it in our update we should pull this out cache it and just have a single reference to the camera but for now I'm just leaving it because performance is the last thing on my mind this early in the game all right so let's see why was he not animating backward let's take a look so we'll open up the animator hit play again and we're just gonna watch the character in here so I'm gonna select him and let's see what his values are so you see his speed goes up if I go negative his speed goes down but he's not is he going into walk backward he is going to walk backward he's not actually playing a walk backward animation so let's see what's going on all right so the problem here is actually on the walk backward animation if we take a look at it you see that it's not set to loop and this happens a lot when a download animations from miksa mode they're not looping so if we look at what it's doing it's just basically just animating that one step backward every time we switch into the backward State and it's not looping over there so if we just check the loop time box and hit apply we're almost good but we do need to make one other change and that changes on our animator so if you select the tune soldier action will stop playing real quick we want to uncheck apply rule motion right now we're not using remotion for moving our character around if we make the switch to do that which is essentially making it so that the animations control the movement it may it makes it really good for lining things up perfectly you get really good footsteps and stuff slightly harder on performance not a big deal when it's just our one character though but if we make that switch then we'll reapply that but for now since we're not using that we need to uncheck that otherwise we're gonna get motion from the animation in motion from our backward and they're gonna cast AK and it's gonna turn into a mess so with that unchecked and the animation on loop we should be able to walk forward and backward just fine so here we go I've got a view of the character run over walk backwards and you can see he's walking back oh his backward motion is really really fast that's where root motion comes in nice because that would be lined up exactly with him with how it's supposed to be so we have a couple options here if I want to slow down his backward movement I can we could just add in an extra control or an extra variable and therefore backward movement speed but we also probably want to adjust that animation in fact actually I think I do want to slow him down just a little bit on the backwards so let's do that first I'm gonna select the player go into player movement and let's add a backward move speed backward move speed and I'm gonna rename this one with f2 and name a forward move speed and I think these variables were like was it 7.5 was the forward that I was using and then for the backward let's see if we want to go 2 maybe 3 seems like a good number and then I think turn speed I'm up like 150 so when I just update the the default value there - all right so let's see in our movement now where's that simple move right here now what we want to do is instead of just multiplying by a forward move speed we're gonna multiply by either forward move speed or backward move speed depending on what vertical is so just do float move speed do use equals and then here we're gonna do something a little bit different you may may or may not have seen this before we're gonna do vertical greater than 0 question mark forward speed and then a pullin backward move speed and semicolon and I'm going to copy this move speed to use and replace the forward move speed reference down here so what's this doing this is giving us a movement speed that's either going to be the four or the backward based on the result of this vertical so what's actually happening is this is getting evaluated this if vertical is greater than zero is evaluated if this statement is true we use the value right after the question mark so if it's true if it's vertical is greater than zero we use this if it's false we're going to use this value that's after the : so we'll use the second one there so if we go to zero or less we use the backward move speed remember if we're at zero though we don't even get into here so here we're just going forward or backward based off of that vertical value and then we'll get a different speed out of it so let's hit play and I should move backward much slower about half the speed a little less than half the speed and then I'll adjust the animation just a little bit to line up oh it actually seems okay I think the if I move this up there like a four that winds up pretty well now you can go in and adjust the animation speed too so if I go to the blend tree this value right here is the animation speed so if I wanted to slow this down to like maybe 0.5 so he walks backward animates backward half speed I could do that and then I would just need to turn down his movement speed even more in fact I think I'm gonna do that so keep him at half speed I need to make him move much much less so let's drop this down like a - every time I click I get that gunshot okay there we go BAM now he's moving back slowly he goes backwards slow forward fast that's exactly what I wanted cool um let's see I think we've gotten kind of long here already already at about a half-hour mark and we can move around we can shoot this thing [Music] there you go we killed it I think in the next one we'll start setting up some enemy characters if you have requests for very specific things though please just drop them down in the comments I went to the third person character controller specifically because people really asked about it and they want to get it you know in this this kind of movement so if you have something that you'd like to see first before or enemy characters and stuff just let me know and I might just reshuffle things around a little bit all right um and again if you like the videos don't forget to share them with friends and all that stuff it really does help us build enough subscribers and it's nice it makes me happy keeps me excited and doing stuff so thanks for that don't forget to share like all that fun stuff and again thanks for watching
Info
Channel: Jason Weimann
Views: 34,725
Rating: undefined out of 5
Keywords: Unity, Unity3D, uNity 3rd person, unity 3rd person shooter, unity3d 3rd person shooter, unity shooter, unity fps, unity cinemachine, unity mecanim
Id: q9vPZiz8XYk
Channel Id: undefined
Length: 28min 38sec (1718 seconds)
Published: Fri Feb 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.