Ammo & Reloading - Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this hair that officially becomes too long in the last video a lot of you guys asked for a way to create ammunition and reloading until that's exactly what we're going to be looking at today and if you don't have a gun script and weapon switching already set up you can go ahead and watch the two previous videos I'll have a card linking to them here so let's just jump into it so in the last few videos we set up this hierarchy where we have the player than the main camera and the weapon holder the weapon holder has a weapon switching script that allows us to switch out these three different weapons that are all child's of that object and on each of these objects we then have a gun script and this is the script that we want to modify so let's go ahead and double click that and if you're using a different gun script that shouldn't be much of a problem this technique of applying ammunition and reloading applies to pretty much any gun script I've ever seen the first thing that we need to do is add a few variables we need a public integer and this is going to store our maximization so max ammo and we can default that to something like 10 then we also need a private integer and this is going to store our current ammunition so that's going to be current amyl we're not going to default that to anything and then we'll also need a public float and this here is going to be our reload time so we'll write reload time and set it equal to something like a second by default cool so now we can go and create a start method and all we really need to do here is add a single line which sets our current ammo equal to our maximum ammunition so right when we start the game or right when the gun is selected you make sure to fill up our ammunition now every time we shoot we of course want to subtract one from our car demo let's go to the place in our code where we shoot in our case that's inside of this shoot method and here we just add a line and you can do this anywhere within the method and it's going to say current ammo - - to subtract one so now our ammo should set itself correctly and update when we shoot and so all that's left is detecting when we run out of ammo to do that we go into the update method we write if current ammo is less than or equal to zero meaning we've run out of ammo in that case we want to go ahead and call some kind of reload function and of course you want to make sure that we do not go ahead and check for input and then fire and so we'll put return in here which means that our code will simply stop here and not continue on to the next if statement then we can make our reload function so let's go down here and write void reload and we'll start by just throwing a debug that large statement saying something like reloading and of course we want our reloading to take a bit of time but for now just for testing let's go ahead and set our current ammo equal to max ammo and then let's save this go into unity and our debug dead log statement should appear every 10 bullets so I'm just going to hold down the mouse once here and there we go it says reloading and if I continue here it's going to keep doing so every 10 bullets so in order to add some reload time within our V load method we need to be using what is called a karate Kuro teens are a bit weird syntax wise and so some of this stuff you'll probably just have to write after me but they are also extremely useful let me show you how they're used so first off in order to use the corrode thing we go to the top and we need to import so say using system dark collections when you create a script notice that this line will be there by default probably the primary reason for that is corrode teens we then go back to a reload method and instead of writing void here a corrode team is of type I in numerator this is a weird word and we won't be focusing on it too much let's instead going here and now add a separate line and so we'll write yield return new waitforseconds and here we specify the amount of segments that we want to wait so you could just put in one here on our case we could put in reload time and close that off so what we have here is pretty much a normal function we can call it from anywhere in our code it's going to execute the things that we write in here however because we've marked it as an enumerator this function is now a Co routine and this means that we can effectively pass a function at any given time so in our case we write reloading to the console we then pass for reload times seconds and then continue on to set our current demo equal to our max ammo the only thing that we have to be careful with when dealing with kuroh genes is that we cannot call them like an ordinary function instead we have to write start co-routine and then input reload and remember to have both the parentheses for start corroding and for reloading here so now when our ammo reaches zero we should call our reload method which is going to display the message weight set our ammo and then we can continue on however there's a slight problem and that is that every frame where we've run out of ammo we're going to go ahead and start this current team and we really only want to do this once so what we'll do is go up here and add a private boolean private pull and we call this something like is reloading and we're going to fold it to false then in our update method we'll go ahead and add another line on top of everything else and it's going to say if is reloading so if we're currently reloading we want to go ahead and just return so we don't want to do any of the other stuff we have in our update method and we want to begin reloading at the top of our reload function so in here we'll say it is reloading to true and when we're done after we waited and set our ammo we want to set is reloading to false so now when we save this and hit into unity and hit play we should see that we can shoot 10 times it will then pause for one second and we can then continue shooting and indeed it does awesome so the final thing that we can add here is some kind of reload animation however we actually have a bug in here that occurs when we start reloading than switch weapons and when we then switch back we cannot shoot and the reason why is that we never finish reloading and so when we go back is reloading is still equal to true but our kuroh team got interrupted and so it will never be set back we'll go ahead and fix this in a moment but let's first add some animation I want to make it so that we can add separate animations to each weapon but in my case here just to make things simple I'll have the same animation for each one so instead of animating heavy rifle and pistol let's go ahead and animate the entire weapon holder do that let's go window animation to open the animation window let's hit create let's go ahead and create a separate folder for this called animation let's save this animation as weapon idle let's also go ahead and create another clip and this one is going to be called weapon reload now we can hit record and for a weapon reload all I want to do is rotate our gun along the x-axis something like that and inside of our work an idle let's also hit record and make sure to insert a keyframe where our rotation is zero now let's stop recording going to the animation folder let's double click on the weapon holder controller and we can now see two animations the default one being weapon idle you can always make an animation default by right-clicking and going set as layer default state and then we also have the weapon reload animation we want to be able to transition from our weapon idle to reload and back to do that we right click on weapon idle it make transition and then click on working reload and we do is the same the other way around however right now we don't have a condition on these transitions and so they'll just occur immediately we only want this to happen when we start reloading and so what we want to do is go up here under parameters and add a new parameter we'll make this a type poll and we'll call it something like reloading this is a simple boolean value that we can control through a script it can either be true or false and we can have our transitions only occur if this value is equal to either true or false so transitioning for weapon idle to reload will only happen when reloading is true and the other way around will only happen when reloading is false we also want to select both of these animation and turn off has exit time this way we don't wait for our animation to play out but we just transition as soon as it happens so we should now actually be able to parent our animator over here hit play and if we now go ahead and check this value can see that we can switch between the two animation states now to control this through script let's go to a gun script let's add another public variable this one is going to be of type animator and we'll just call it animator then when we start reloading down here we'll say animator dot set bowl and the boolean we want to set is the reloading and we want to set it to true then we wait for a reload time and then we can set this ball back to false so if we now save that and go into unity we can see that an animated field appears for each of our weapons I'm just going to select all of them and drag in the weapon holder if you had separate animations for each weapon you would drag in the animator that sits on that particular weapon and if we now hit play start shooting and you can see it reloads awesome only one thing is that you can actually start shooting a tiny bit before the reload animation is totally done and that's because we can start shooting as soon as we set the pool reloading back to false but there's also a bit of transition duration there by default it's 0.25 seconds so what we can do is actually wait for reload time minus 0.25 seconds then set reloading to false then wait again but this time only wait those 0.25 seconds it's just a quick workaround but it works beautifully and now we should see that it only starts firing when it's totally done so to fix a little bug here we're if we go ahead and shoot and reload and then change weapons you can see that it kind of messes up the animator and when we then switch back we aren't able to shoot everything is being a tiny bit weird to take care of that we go into visual studio we go ahead and create yet another method and this one is going to be void on enable and in here all we want to do is set is reloading to false and we also want to go animator set ball reloading to false as well so the only difference between the start method here and the on enable is that start is only called the first time the game object is enabled well on in abel is called every single time and so this is one of those fairly rare cases where you actually need both and we should now see that when we begin shooting start reloading switch weapons it goes back to normal and we can now shoot with the other weapons as well and when we then switch back to the weapon it starts reloading again and everything is good and so this should very very easily work for all of our different weapons and you can see they simply use the same animation I know but you can go ahead and hook it up to different ones and you could also go ahead and expand upon this with some kind of UI displaying the current amount of ammo lots of possibilities here let's pretty much it for this video if you liked it make sure to subscribe so you don't miss a future episode also I'm really excited about the video that's going to be airing this Sunday it's going to be a behind the scenes video showing the making of my recent entry into the Gotham tower 38 combo had a blast making it and I hope you guys will enjoy I'm that thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donate it in March and a special thanks to Derek Eames Kirk faceful marrow 5 james Calhoun and Jason Lotito if you want to support the channel and become a picture in yourself you could do so a patron the cons - brackets thanks a lot guys
Info
Channel: Brackeys
Views: 218,536
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, beginner, easy, how, to, learn, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, ammo, ammunition, reload, reloading, bullet, bullets, count, animation, animator, switch, coroutine, fps, first, person, shooter, thrid, shoot, gun, weapon, system
Id: kAx5g9V5bcM
Channel Id: undefined
Length: 10min 26sec (626 seconds)
Published: Wed Apr 26 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.