Unity 2021 Tutorial - WEAPON PICKUP & VFX - FPS EP 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This post appears to be a direct link to a video.

As a reminder, please note that posting footage of a game in a standalone thread to request feedback or show off your work is against the rules of /r/gamedev. That content would be more appropriate as a comment in the next Screenshot Saturday (or a more fitting weekly thread), where you'll have the opportunity to share 2-way feedback with others.

/r/gamedev puts an emphasis on knowledge sharing. If you want to make a standalone post about your game, make sure it's informative and geared specifically towards other developers.

Please check out the following resources for more information:

Weekly Threads 101: Making Good Use of /r/gamedev

Posting about your projects on /r/gamedev (Guide)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

👍︎︎ 1 👤︎︎ u/AutoModerator 📅︎︎ Feb 04 2021 🗫︎ replies
Captions
[Music] hi this is michael from expanse vr and in this episode of my unity 2021 first person shooter tutorial series i will be looking at the fun stuff picking up a gun and shooting things and if you're just joining me for the first time click above and you can actually start the series right from the beginning where i show you how to do everything from scratch or if you just want to watch along please do um i do go into depth about most things but there will be a few things that i will be skipping over as we go through this episode having already covered it in other episodes and if you're new to the channel don't forget to like this video subscribe to the channel and of course click that notification button down below to make sure you are notified whenever i post a new tutorial for you now in this episode we are going to be using a couple of free assets from the unity store so before getting stuck into the coding side of things what we need to do is bring in this first asset which is this sci-fi gun by these guys factory of models they actually have two free guns that they have for offer i'm going to just use this one for a start so open in unity and i have noticed with unity 2021 or at least my version of it that you do sometimes have to click the link on the store a couple of times there we go so just click import now with this one here we don't need that one but we will take everything else in so we're going to use this for a couple of different things first of all we're going to need to put it on our player so that we can have a gun to bring up so if i just jump over to our player hit f to bring him up or her up now if we go into our sci-fi gun light they actually have a couple of different colors that we can go from so you just click on prefabs gun light so as you can see we've got a couple of different colors here and the first thing you'll notice is that it will come up as purple so we just need to upgrade it remembering that purple just means that the there's a shader error and all we do is upgrade project materials to universal to urp don't worry uh it sometimes takes a little while for that to refresh but it will be there so there we go we can see here now what we need to do is just line him up to where we want him to view so for me it's about right sort of looking forward i always feel that they kind of face inwards a little bit and again this is your personal preference you can even have it rotating up slightly but i think that gives it off so yeah there we go and then all we do is we child it to our player so if we play now there we go he's sitting with us now the problem we have here is that it's actually moving with our player and not with our look so we actually need to tile this to the main camera so trial again try it again there we go so now where we look is where our guns can be pointed at perfect now the other part we need to do is actually have the gun somewhere where we can pick it up oops what is happening there why is my sorry if my navigation is a bit off today i'm used to using blender and zbrush and sometimes i get confused about how to navigate around this okay so what we're looking for here is a spot where we can stick our gun for pickup now you can put it anywhere you want in the scene me personally actually what i'm gonna do is just stick it here in the room right at the front so i'm just gonna stick it here bring it up and if i remember correctly i set this door to being locked so for the moment i'm going to unlock him is local nope and stays open just for the moment so now come into the scene there's our gun for pickup now it's the same size as what we've got but you might find that a little bit hard to see so if you want you can actually make that a little bit bigger which is what i might do just to make it a little bit more visual not much and we just hit that r key for the scale gizmo okay so next up we're going to do is just make our pickup item just a little bit more interesting and we're going to start that with a small little script that's going to be great for you to use on lots of other different objects that you want and we're simply just going to call this rotate object pretty self-explanatory about what it's going to do and it's great because it's just a couple of lines of code that you can just attach to any object that you want so opening it up in visual studio we don't need this one here so we're just going to have a couple of different variables first one is rotate y and this is the one that we're going to use for this particular object uh what i've done float sorry float and rotate x you can do a z1 as well if you like but for the moment i'm just going to keep these two because that's the main ones that i will actually know what the hell let's just keep it in there because we might use it later on for something else and i just realized i should have had it that way because it makes more sense now the other thing you can also do with this is we can actually make this a lot cleaner by going rotate y root uh rotate zed ah actually i remember we do it this way just remembering though that if you do do it this way that if i had a float for something else entirely um in the script that's not related to this rotation part that is not usually the best practice to put it in here but because all three of these pertain to the same thing and are but are afloat this works and makes things a little bit cleaner so all we need to do here is transform dot rotate new vector3 and just simply rotate rotate x my typing is the west at the moment and this will be times time dot delta time and to be honest if we would make this a little bit more readable and a little bit cleaner we probably should do something like vector 3 new rotation equals what we just created there and then new rotation just makes a little bit more readable we don't need any of this and we don't need any of this and i just realized i misspelt that oh and in case you didn't know what i just did there if you right click on a variable and click rename you can rename the variables um all through the scripts so they reflect the same name i don't know if i've actually mentioned that in another one so i thought i would now so jumping back into unity if we click on our sci-fi and gun there add the rotate object and simply push play we should see it now rotating now the issue that you see here though is that the axes of the the rotation point of the object looks like it's somewhere around about here so it's kind of rotating in an odd way so what we're actually going to do is remove that creating empty this will be our psy gun pickup if we click on our sci-fi gun copy the position paste the position and there you go you can see that it's sort of sitting in the wrong spot there move it sort of more into the middle here so it makes a bit more sense and then we'll drop the gun into here add a rotate object and there we go nice and as you can see if we want we can do some funky things like that if you want to so now we've got this cool little rotate object script that you can add to anything that you want very easily so again that's not actually necessarily i just thought it'd be a little cool thing to add to it so i did now before doing the next step which is actually uh picking up the gun what we're going to do is stylize this a little bit more and i must admit the next step is probably a little bit over the top um but it does introduce people especially if this is the first time using unity or you're very new to unity this is going to be your first introduction into the particle systems which play a very big role in games with unity actually games in general so again we're going to use a free asset from the asset store and both these asset store um ones that i'm using i will provide a link down below actually there's going to be another free asset that we're going to use as well yep it's just free asset day today um so this one here is this particle system from skj um they have a few cool other ones in there as well though i don't think they've actually done anything for a while um it does say unity 5.3.5 but it is mostly just a particle system so it does work in unity 2021 so once again open in unity do it again and import and in this instance we will we want the animations the materials the models the plugins the prefabs uh we don't need the scene we do need the textures you can keep the readme if you want to have a look and just click import again now this comes up here again just telling you that has been api change that's fine um just click yes um just for these files cool and we're done so this is found here in the particle ones we're looking again for the prefabs i'm going to be using the magic ring i think it's the first one or the second one i'll find out in a second when we put it out there nope not the blue one again this is your preference you can use whichever one you want i think i just the same one again yes i'm looking for the red one red one to go with the red gun so as you can see it's a little bit big so pulling it in again we had the position before so actually no we needed the position off the sci-fi pickup this time so again copying the position as you can see it's going to affect the rotation which is really weird i didn't click on rotation but it didn't anyway again hitting the e for the rotation tool if you hold down control you'll constrain it to 15 degree increments and we'll hit 90 degrees hitting w again to bring down the gizmo and just sync it all the way until it just disappears and then bring it back up again there we go cool as i said you're looking at now and you're going this is over the top i did warn you that this is slightly over the top for it but i think it looks cool and it's like i said it's good way to introduce you to the particle system now if we have a quick look at what's going on with this particle system itself um over here and by the way we can drag this now into our sci-fi gun pickup it is a prefab with one two three four particle systems and in fact i think it's five yes five systems making it up and actually the top layer even has one so it's actually technically six particle systems now the problem we have we can't just simply change the scale here it's not exactly how particle systems work and if it disappears like that just click on this one here and click play and it'll keep going i'm not going to go too in depth into how particle systems work in this one here i will leave that for a different tutorial probably one of the how to series or i might even start up a particle system um series of its own just follow along for this one here and it will give you sort of a bit of an idea and i will try to explain what is going on so the first thing i want to make sure that happens is that we scale it down a bit which is this one here the start size so we need to bring it down that's not affecting one there again start size there we go that's affecting that little white ring there so we'll bring that one down probably to about there maybe even smaller yep that'll do same with this one here start size 2.5 so that's that little red ring this one has a variable start size so this one's saying that they can start from anywhere between 0.5 and 3. so we're going to half both of those okay so with this one here we will change that one here but we do need to change another thing to make it smaller this is the emission oh shape so the shape is actually how the radius of it is so if we bring that in now there we go that's about more right and i think that's what we're going to find with this three here is that we actually need to change the shape on that one as well so the shape is actually what is emitting the particle so here you can see that it's a cone as a circle and we just need to bring that down to line it up with everything else that's going on but same again we are going to change those shapes because they are too big with what's going on and there we go so now it's all confined to the same space as what we had now let's find out what does what for so if we get rid of this see that by itself is almost okay it's really this part here that is sort of making things a little bit too much let's see if we can do that so we can have a look at two things here might be the start color or color over lifetime let's check this one first color over lifetime is basically as it's self-explanatory the color that it displays over the life of the actual particle itself let's see start color i think from memory yes so color over lifetime actually overwrites it and what we're going to do here is we're actually going to bring down that opacity just a smidge and same with this one in here this is barely anything anyway so there we go still quite there i still feel like this is a little bit too much for what we're trying to do i still think we need to bring that down just a smidge more so it's just a hint of a color there we go and what about this ring where's this ring coming from is it this one here yes this white ring what just happened to that white ring why has it disappeared on me cool uh is this the color of the lifetime yes okay let's reduce this down and this reduces down two is that uh because the duration is ridiculously long there we go that's a little bit better and i think we've got this final ring which is that red ring i think that one as well we can just reduce that down slightly there we go okay that was a very very long process and it's up to you just muck around with it i can tell you now particle systems are just one of those ones where you just need to keep playing around with it trying different things break it unbreak it and the more you use it and the more you play with other prefabs which is why i decided this way was a better way playing around with someone else's work to see what they've done and then try and muck around with it is a good way to learn what each thing does okay so now that we've got that working let's make a prefab of our sci-fi gun pickup so that we can use it at a later time so into our prefabs folder sci-fi gun pickup goes in here so now we've got that there and what we can do to clean this up is we're actually going to create a no we'll do that later on um okay so the next thing that we need to do is actually go to our player find that sci-fi gun and actually disable it so that it's not visible so we want to start the game off with no weapon and then we're gonna walk through that door find our first weapon and go shoot some things so jumping into our scripts folder we're actually going to start tidying things up a little bit so for the next two ones that we do we're going to create a folder called items and we're going to create two scripts in here and this is where we're going to see oops not folder create script item create script weapon and this is where we're going to start showing you how inheritance works if you haven't done it before again i should be doing a how-to series on inheritance at some points but for the moment we're going to briefly touch on it and you'll see how that works so before i forget we will attach to our sci-fi pickup weapon and then the first one we do is jump into the item script now being a base class we don't need to do too much um but the first thing we need to do we won't be needing these and we will do a lot more with this class later on but all our items will be inheriting from this one and the first thing we're going to do though because it's going to be inheriting is that we're going to call this abstract now there's a little bit more to it but basically what an abstract class is and the reason why we use it is that you cannot instantiate this class directly you actually need to have another class inherit from it and then use the functions and the variables from this particular class so you'll never attach an actual item script to any of your objects you'll as you'll see we're going to make a weapon class and we're going to inherit from this class now the only thing that we're going to be doing because all items that we're using can be picked up is that we're just going to create that functionality and what we're going to do is an on trigger and we're going to say this dot game object is false now if you want you can go in and just do a destroy object but we may have a few other things later on like certain things that pop up over time or may move around so it's better just to get in the habit of deactivating it and the other part we want to do because we may want to use this on trigger enter function and some of our other objects is we just need to change this to a virtual protected function now the first part of this method is the virtual part of it and simply put a virtual method just allows any subclasses and a subclass is a class that inherits from another class so for instance the weapon class that we're going to create it just allows them to override this particular method as needed and the second part is the protected keyword a protected member is accessible within its class and any classes that derive from them so now that we've got that item base class sorted we are now going to create our weapon class and the very first thing that we do is that we change this from a mono behavior to an item and the thing is with our item class which i need to say before i forget it already inherits from mono behavior so this item this weapon here will inherit from item which inherits from modern behavior so we get all the functionality of my modern behavior plus this functionality of item so basically when we use monobehaviour we're doing the exact same thing as what we're doing here now with item and weapon and this is what inheritance essentially is and is one of the fundamental components of object orientated programming now again we're not going to have too much involved in this one here and again we don't need these ones here so i'll get rid of it and we don't need either of these in fact as well so we're going to be creating pretty much everything from scratch the first thing that we are going to be doing is though an enum weapon type and again if you don't know what enums are right above now you'll be able to see the link to my how to video on enums which explains in depth about how these work so here we're going to have two declarations bulk gun and laser gun and the moment we're not gonna really worry about the laser gun what we're gonna be using is the bolt gun but it just gives us an idea of what is there we're gonna create a public sorry serialized field of weapon type and this is just going to allow us to declare what sort of weapon this is in the inspector and now we're going to get to our on trigger enter and you're going to see that this is going to come up with a bit of a notification letting us know that we already have one ontrigger enter inherited so what we actually want to do is instead of having void sorry before void we're going to do override so we're going to remember what we said with the item when we have it as virtual we can override it now this is going to cause an error though because it's going to tell us that the where are we that there's a mismatch between what the access level is for our item on trigger enter and for our weapon one and all we need to do is change this from private to protected and we're all good now what we want to do here is we want to utilize what's going on in here already so what we do is call base which means this base is referring to our base class so it's going to call everything in here on trigger enter and we need to pass in that collider because this one here requires a collider as well so as we add to this here it will automatically be called from this base on trigger enter method call here and the only thing that we really want to do at this stage is check to see if the other dot tag equals player i don't know why they just order field like that and just checking yep we've already got it set to players that's good and what we are going to do here for the moment is just print uh how about weapon type picked up so now if we jump into our game all being equal what should happen now is that our gun should be destroyed but it's not why is it not okay just check the console no errors the sci-fi gun has its weapon script ah you know it will help we actually need a collider and in this instance i am going to use a sphere collider yeah that looks pretty yeah i think that'll work that there and is trigger so now we should work door opens boom there we go we've now picked up our item we just now need to let our player enable the gun so we just need the player now to enable hit the gun that he's holding so going back to our scripts we're actually going to create a new folder called player and we're going to place the player controller in there and we're going to create a new one called player combat and we'll assign that to our player and what we're going to do here as well is we're actually going to make a prefab of our player as this will be needed later on and as a quick clean up in here we're going to create a folder called pickups i'm going to place this up here i like to keep it uh alphabetical and we'll place our pickup in there and in case you're curious what this is here this just means that once we click on this one here you can't actually select these items in the scene so this is what stopped me from accidentally clicking on our actual scene but pickups i've still got it so we can so if i want to i can pick up on our object but nothing else so going back to our script folder to our player let's launch our player combat script let's open up our player combat script now we won't need this one but we will need the system collections at a point now the first thing that we need to do is we're going to create a serialized field to store our guns that we are holding so this is going to be an array of weapons well actually sorry it's a ray of game objects called weapons and we are also going to need a couple of other components but worry about them in a second the thing that we need to do we won't need the start function but we will need the update one for when we do the firing so we are going to create a public method called set weapon and we already know that we're going to be able to have different weapons that we pick up and utilize so what we can do is utilize that weapon enum so weapon dot weapon type and we will pass in the type of weapon that we're setting and for the moment we're only going to have one weapon so we're just going to use a simple if statement and in this case we're going to say if the weapon type that has been passed in equals weapon dot weapon type dot bolt gun we can then look at our array and the moment we've only got one item that we're going to store and we'll set it to true again if you're a little bit more advanced and you're following along with this you're going to go hang on there are better ways to do this we will get to that later on but for the moment for the sake of what we're doing here we will use that and if we save jump into our weapon class we're going to change this to other dot game objects and i know i said not to use get components but for the sake of prototyping as we do know the better way of doing this we are going to use a get component and what we are going to use player combat dot set weapon and it will be this weapon type and the only other thing that we're going to need to do now is go to our player and in our weapons we'll add one to it and we're going to drag in our sci-fi gun oh yellow i thought the color was a little bit off that's fine we'll use that for the moment you get the idea so now if we push play we're gonna walk through there's our player pick up a gun and there we go we got it in our hand awesome stuff so that is it for this episode uh at the beginning of the video i did mention that we would be shooting our gun but because we sort of dragged out to over half an hour i am going to leave that to the next video and we'll get stuck into it looking at a particle system for the gun firing how it fires its interaction with objects around us so thank you very much once again for joining me and if you want to discuss this episode or of any other unity project you're working on please feel to join me on my discord channel the link is below and thanks for watching don't forget to like and share to help grow our beautiful unity community and don't forget to hit that subscribe and notification button below to make sure you get to see our next video see you shortly
Info
Channel: ExpanseVR
Views: 1,301
Rating: undefined out of 5
Keywords: unity3d, first person shooter unity, unity 2021, unity3d tutorial, unity 3d, unity tutorial, brackeys, unity3d 3rd person shooter, unity first person controller, unity game development, unity fps controller, unity first person shooter controller, unity 3d tutorial, unity tutorials, unity tutorial for beginners, indie game development, how to make a fps game in unity, how to make a first person game in unity, unity game dev, unity gravity, unity trigger, unity events
Id: sInDMZaJ2yE
Channel Id: undefined
Length: 36min 37sec (2197 seconds)
Published: Thu Feb 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.