POWER UPS in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Hey Brackeys can you make a video on what to start with and how to start...like tips, from beginner to professional. Not like your normal videos where you make tutorials but a video where you talk to the camera. But not only about how to start...the whole journey like after beginner when you like become intermediate too. Oh and im 13 years old...so can you put that on the video too, like advice for people who are still young and wants to start?

👍︎︎ 3 👤︎︎ u/[deleted] 📅︎︎ Nov 16 2017 🗫︎ replies

Hey Brackeys, nice video. Though I've got a question. So I made a gun pickup that is activated by a button press. However I only want to pickup one gun at a time. How can I solve this? (there can be instances where 2 pickups both collide with player)

👍︎︎ 1 👤︎︎ u/H_justice 📅︎︎ Nov 16 2017 🗫︎ replies
Captions
in this video we'll have a look at how to create power-ups in unity not much to say about that so let's just jump into it so when it comes to making a powerup it can really be split up into three stages first we have some kind of object that will register whenever a player gets near it then when he picks it up we apply some kind of effect to our player this could be making him bigger give him more health or give him a new gun and then finally we create some cool particles and remove our powerup from this scene let's start by focusing on the first step so as you can see I have a scene here in unity where we can move around our player and the first thing that we want to do is create a powerup for him to pick up now you can really use any model here I've created a simple one in blender so here it is in unity all we need to do is drag this into the scene I'm gonna focus on it in the scene view and I'm gonna drag it down here I also want to make sure to apply the proper materials so I'll drag in the center here so now we have a simple model that we can apply our script to let's select our powerup that's it add component and the first thing that we want to add is some way for unity to detect whether or not we're colliding with other objects so we'll of course create a sphere Collider will open this up and we can now adjust the radius to whatever we want I'm gonna make it a bit smaller than the model I find this often looks the best let's also make sure to mark this Collider as a trigger this way other objects won't actually collide with a powerup but our powerup will still be notified whenever an object is near so now we should be able to play the game and of course whenever we collide with our powerup nothing currently happens to change this let's go and add a new component let's create a custom script called powerup let's make new script it's like c-sharp and they create an ADD and let's double click to open it up in visual studio now we can go ahead and delete both the start and update method instead we want to be using another callback method used by unity I'm talking about ontriggerenter so that's right void on trigger enter and you have to spell it the exact same way that I did here in order for it to work well then open and close some parentheses and the curly brackets now this method is called by unity whenever another object enters or trigger I'm gonna actually get some information about the object do that we go up here and write collider because we want to know more about the collider we hit and we can then call this Collider other then we want to make sure that whatever entered our trigger is a player to that we could use the tack of the collider so if we write if other dot compared tag we can check if the tag of the collider is player and if it is well then we are ready to go ahead and pick up our powerup will make a separate method for that down here void pickup and this is of course where we'll actually pick up a powerup so now we've actually taken care of the entire first step we have a function that gets called whenever we collide with something we check if that something is the player and if it is we go ahead and call the pickup method then we'll take care of the second and third step inside of this method for now we'll just write debug the lock powerup picked up just a little self note that this is working let's now save this go into unity and now we of course want to make sure that our player is also tagged as player to do that you go to the tag here at the top and you make sure to select player if you want to use a custom tag we go and do your add tag and you can add any tag that you'd like here if we then go back and select the player we can now use that tag so now we should see that when we hit play and move into the powerup it says powerup picked up of course nothing else happened but that marks the end of step one next up we'll focus on step three which is spawning some cool particles to let us know that something happened as well as removing the power up from our scene to do that let's go into our code and the first thing that we want to do here is spawn a cool effect we then want to apply some kind of useful effect to the player and finally we want to remove the power up from our scene and the first and last part is actually super easy it's the exact same method we use to in creating a hand grenade if you haven't checked out that video I definitely recommend you do to remove the powerup object we simply call it destroy and we input a game object which means the powerup itself to create a cool effect we first need a reference to the effect to do that we'll go to the top here and create a public game object let's call it pickup effect then inside of our pickup method will instantiate the pickup effect at our current position so transformed our position and using our current rotation so transformed rotation if we now save this go into unity select our powerup we now see that we have an empty slot for the pickup effect now I'm using the Unity particle pack that you can get free of the asset store I'll of course have a link for that in the description so if we're now going here we can search for the different effects in the pack and I've taken one of these effect and modified them a bit too a cool power pickup effect I haven't done too much I've basically just switched around a few colors so if we now hit play and try and pick up the powerup we can see that it disappears in a cool puff of lightning and smoke and everything so cool but of course our player is still just as good as he was before which of course brings us to the second step adding effects to our player and we look how happy is and we've already pinpoint the place in our code where we can do this and of course this part is going to completely depend on what you want to modify about your player I'm gonna show you a couple of examples say we wanted to make a player bigger well in that case would go in here and we'd first need a reference to our player up here in our ontriggerenter we actually have a reference to our player and that's the other variable but down here that's disappeared so we need some way to pass this other variable into a pick up method so that we can use it down here to do that we'll use a function argument so just like we write collider other up here we can now write Collider and we could call it other here as well but I think we should call it player instead since at this point we know it's a player and then here when we call our pick up method we'll simply say that our player should be the audio object this way we are taking something from one function and passing it on to the next and now inside of our pickup we can simply use player transform the local scale to modify the scale of a player and I want to go ahead and multiply it by some value and instead of hard-coding this in let's go to the top here and create a variable for this let's create a public float called multiplier I will just default this to something like 1.4 then in our pick up method we can simply write multiplier so now after instantiating our particles we should see that our player gets 40% bigger and then we move on to destroying the powerup object let's try and save this and if we now play and pick up our powerup we can clearly see our player gets bigger awesome but of course you might not always want to just make your player bigger say if we wanted to change some stats on on player so just give him more health well let's have a look at how to do that in this case we would probably have some kind of script sitting on the player called player stats then I've gone ahead and created a very very simple player stat script if we open this up all that you seen here is currently a public float storing the amount of health our player has now this is of course a ridiculous example but the main thing that we need to worry about right now is how to make this health bigger using our power well since this player stats component actually sits on a player object we can simply use dot get component of type player stats in order to get a reference to it we can then store this reference inside of a player stats variable that will call stats and now we can use stats dot health in order to modify this variable and just like we did before we can simply multiply it by our multiplier variable so now we should see that our health go from the default 100 all the way up to 140 if we save this and go into unity and hit play we can keep a look at our health variable over here and we're now now into the powerup we can see that it pumps right up to 140 that's how you can make changes to your player by picking up these power-ups but often you don't want these changes to last forever it's very common for power-ups to only last X amount of seconds when that case will need to go in and modify our script what we need to do is go in here and wait X amount of seconds we then need to reverse the effect on our player and then we can go ahead and destroy the game object well this is actually fairly simple in order to reverse the effect on our player all we need to do is go in here and say stats dot health and this time instead of multiplying by a multiplier we'll simply divide by our multiplier and now our health should be back to normal but in order to wait X amount of seconds we actually need to change this method from an ordinary function that returns nothing into what we call a co-wrote team because carotenes allows us to pause and wait if you've never dealt with kuroh teams before you might find that the syntax is really weird and that's probably because it is but for now just write after me so we'll go and replace void here with I in numerator and now we've actually marked this as a current team then when we call the method up here we need to add a tiny bit of extra code and that is the start co-routine function and the kuroh team that we then want to start is our pick up method so we need to wrap this in two parentheses and now we've actually set up a karate now all we need to do is go in here and tell it to wait to do that we'll use yield return new wait four seconds and then inside of these two parentheses we specify the amount of seconds that we want to wait we can wait one second two seconds three seconds you get the idea and of course we want a wait time to be specified by our variables let's go up here and create a public float called duration and set it equal to something like four seconds by default then we'll input our duration down here and this should actually work however there's one slight problem if we now save this and go into unity and hit play you'll see that when we pick up our powerup it does change our health but our powerup is still visible until of course the duration runs out and then finally our powerup disappears but we have to wait until we've done all of this stuff before we can destroy our game object so what we'll do here before we wait is simply disable all the graphics on a powerup as well as the colliders so that we can't collide with it again this is way to do this is get the two components I'm of course talking about the mesh renderer as well as the collider to do that we'll use scape component of type mesh renderer as well as gate component of type Collider and now we can simply use dot enabled in order to change the state of these two components and we'll of course set enable to false make sure to do this for both of them so now we should see our powerup gets disabled as soon as we pick it up for the final time that save this hit play and we should now see that as soon as we pick up the powerup it disappears our health goes to 140 and after four seconds it changes back to 100 and there we go we now have a powerup system implemented inside of our game that's pretty much it for this video if you enjoyed it make sure to subscribe so don't miss a future one thanks for watching and I will see you in the next video thanks of the awesome patreon supporters who donated in October and especial thanks to do teaming armen hence off-tune Kookaburra superman the great James P Thomas Wally Sybok mommy Jason the Tito Derek James Kirk faithful Murphy Manolis Nick Lang Aaron Robert bond and Peter Locke you guys Rock
Info
Channel: Brackeys
Views: 240,667
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, assets, model, texture, models, textures, material, materials, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, game, development, develop, games, programming, coding, basic, basics, C#, asset, power up, power, up, ups, power ups, health potion, potion, potions, upgrade
Id: CLSiRf_OrBk
Channel Id: undefined
Length: 10min 54sec (654 seconds)
Published: Wed Nov 15 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.