How To SPAWN ANYTHING in your Game with Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
whether it's items upgrades enemies platforms ui elements special effects if you can create it in the hierarchy menu you can create it through script and what drives that is using the instantiate method typically with prefab game objects so let's take a look at how we could create our own spawner prefab to use as a tool to expedite when we want to spawn or create objects on the fly in our games i set up an example scene in a 2d unity project and off the bat i have this barrel object that i want to spawn in my game so let's take a look at how we would approach creating a spawner i'm going to right click in the hierarchy and create an empty game object and call this spawner for now i'll get rid of this barrel but i have it as a prefab the object can be anything like i said in the intro whatever prefabs you have it's irrelevant it should all work the same all right so i'll call this empty gameobject spawner and up here in the top right you can actually select on this because i don't plan on having a sprite for it but you can select one of these icons and in the editor this just gives you like a visual representation of where it exists so you can keep track of it we'll then go ahead and create a new c-sharp script and call this spawner and we'll make sure we drag that onto our spawner object let's open that script up and start talking about how we want to construct this thing the first thing we definitely know we want is a game object or a prefab we want to spawn so i'm going to start with a public game object and aptly call it object 2 spawn and let's start real basic for now we'll just say when the game starts we want this to spawn our object we'll take a look at what actually drives that which is the instantiate method in unity and if you open up the parentheses you'll see that this method has actually been overloaded 10 times we won't cover every single one but let's take a look at some of the more common uses you'll see in the argument list the object we want to spawn the vector3 position of where we want it to spawn and the quaternion rotation it could also take an apparent of an object you want it to spawn on and most of the other variants are pretty much the same thing they all basically want a position or a parent for you to spawn an object on but the purpose is the same let's take a look at two differences for now we'll put in object spawn because that's the object we want where we want to create the new object is this spawner game object that we set up before so it should be right here and to that we can just say transform.position because this script is attached to the spawner game object and so that's the spawner's position and so now for rotation there's two common ways of doing this you can either do transform dot rotation this takes the rotation of your spawner game object just like the position and so right now our spawner game object rotation is zero and so if we were to drag this barrel launcher into object to spawn and hit play when the game plays it spawns in the center as we expect and you'll notice the rotation is the same but if we were to actually rotate the spawner say 45 degrees in the z-axis and hit play again you'll notice that the barrel is now rotated 45 degrees it doesn't really make much sense with this spawner example but if there was an object you're spawning from that you wanted to be rotated say a weapon on the player or something like that but you wanted whatever you're instantiating to keep its prefab rotation so let's say on the barrel prefab we set the rotation to 90 and when we drag it in we see it looks like this if we wanted to ignore the spawner's rotation and just use whatever the prefab has and how we set it up then instead of transform rotation you could do the object to spawn.transform.rotation and so with 90 degrees on the prefabs rotation when we hit play as you expect it rotates in 90 degrees and a third very common thing you'll find a way of doing it is even though the spawner set to 45 degrees and the prefab itself is set to 90 degrees if in our script we decide to use quaternion dot identity this will actually align your game object with the axis in the world and what that means is it will actually ignore both rotation settings and when we hit play in this case it points it straight up this is backed up by the official unity documentation which says this quaternion corresponds to no rotation the object is perfectly aligned with the world or parent axes another thing you could do is create a new variable for a spawn 2 object and in unity we could just create a new empty object i'll give it an icon so you can see we can just put this object like over here say you wanted to spawn some enemy or some item or something on this ledge you could put a game object there you could pass this new game object into your spawn 2 object which probably could be called something better in your instantiate we'll spawn this object and then we'll pass in respond to object.transform and so it will actually make the barrel a child of this new spawn location and so if we hit play we'll see that our barrel just spawns the prefab right where we told it to and notice since we didn't pass in any additional arguments that it used the original rotation of the prefab that we set before which was 90. many of the other different overloads are just different combinations with between but basically you have your object to spawn the position you want it the rotation of where you want it and also the parent you want to spawn it under maybe for this example i'll just leave it to transform position transform rotation which is our spawner object we'll reset this transform of the spawner to get rid of that rotation we did earlier okay so instead of a barrel let's use a new object i'm going to use this new fireball prefab that i've made in one of my older videos again it's irrelevant what it is it basically just moves and explodes it doesn't matter but i have it as a prefab and our spawner will replace the barrel with this fireball in this example let's look at how we want to spawn this object on a timer and to do that we need two floats so the first one will be a public float time to spawn so this will be like the max time and then we also need a private float current time to spawn which we'll use to track our cooldown in our update method we could say if current time to spawn is greater than zero then current time to spawn minus equals time dot delta time else if our time is less than or equal to zero we want to spawn our object and then we want to reset the time so we could say current time to spawn equals time to spawn in our spawner script we could set temp to spawn equal to let's say two so two seconds and when we play notice it spawns right away and then every two seconds it spawns a fireball now this could also be let's say an enemy you could use this for a lot of different things you could make it an enemy spawner and obviously make the time a little bit longer or maybe not or platforms that you jump up and down to that's like in mario or you know power-ups anything you can think of but like i mentioned when the game starts you notice it spawns right away if you wanted it to go through its cool-down cycle first in your start method you could set current time to spawn equals time to spawn with this in play it won't actually fire until it goes through the full timer first we'll extract this out into its own method called update timer and if you wanted to you could add like a public bool call like is timer or something like that and basically just say if it's timer then you want to update the timer you know otherwise you'll just have to manually control when you want to spawn it in your script you can toggle yes or no to this and you can also call this from other scripts as long as spawn object is a public method then you could go ahead and call this from any other script you would like which means if you set up unity events or you can make a trick responder class where you take in a public spawner and say void on trigger enter 2d with a collider 2d you can basically say if the other game object compare tag is player so if the player steps on this then we want our spawner to spawn object and i'm just using this sprite here but we basically could make a new spawn trigger and i'll just drag it over here give it a circle collider set is trigger to true give it the spawn trigger script and drag the spawner into the public variable so now when our spawner walks into this trigger it shoots off the fireball you could do the same thing with interacting with an item or an object the same approach applies it just wouldn't be on trigger it would be based on the event calling it we covered a few different ways of how you can go about spawning these things but the spawning itself is a little dull so let's try and spice that up using unity's particle system i created a little spawn effect and i also imported this explosion sound effect so in our script we can make two new variables a public gameobject spawn effect and a public audio clip sound effect we can make a new private method void execute spawn effects and here we'll instantiate the spawn effect at the spawners transform.position and the spawners transform that rotation just like the fireball and for our audio clip we'll play it at the location of the spawner and to do that you can just do audio source dot play clip at point and we'll put in our sound effect at the transform dot position then we can just call execute fun effects we'll drag in our spawn effect and our sound effect and just like that you have a sound effect and a particle effect being played every time you spawn in your object yeah and i know it's probably hard to beat this aaa status effects going on right here but i'm sure you'll come up with something for your own that looks just as good as this the last thing i want to show is what if you wanted to have a list of objects to spawn and randomly pick from one and to do that is pretty easy we instead just want to have a list of game objects and call this objects to spawn and create a new list of game objects we basically just need one more bull as a flag we can toggle in the inspector to say choose random object or not so we could say public bool is randomized down in our spawn object we could say in index is equal to is randomized question mark random dot range zero comma objects to spawn dot count colon zero then we just wanna make sure that we actually have some objects in our list so you could say if objects2spawn.count is greater than zero then instead of instantiating object to spawn we want to instantiate objects to spawn and then open the bracket and put in index this syntax here is a ternary operator and it basically says if is randomized is true we then set index to a random range between zero and the count of the list otherwise we just set it to zero which is the first object in the list and so with this i could actually drag in the fireball and i could drag in the barrel and pretty much any other object i would want and we can set is randomize is equal to true and so when we play we should either get a barrel or a fireball and so we get the fireball and we get the barrel and so it's randomly picking between the two and if we set is randomized to false since the fireball is first in the list when we play again it should just be a fireball every single time which it will be lastly you could just set some headers to clean up the parameters of your component when you have a lot of public variables like this it just helps keeps it organized and it will break up these sections in your actual component itself so we covered the basics of how to create and instantiate objects in the first place and all the different configurations and arguments you can pass in with that we covered a few interesting ways of how you can call your instantiation and how to randomly instantiate objects from the same script and also how to spice up your instantiation to look and sound a little better which mine are clearly incredible if these tips helped you out then please give the video a like takes a second and it really helps me out if you have any questions and join our discord channel or leave a youtube comment thanks for watching and if you enjoyed this kind of tutorial and you're looking for more then then you know what to do [Music]
Info
Channel: BMo
Views: 12,784
Rating: undefined out of 5
Keywords: how to spawn in Unity, Unity tutorial, unity2d, unity3d, learn, bmo, instantiate in unity, spawn enemy, spawn enemies, c#, beginner, object pooling, Spawner, sound effect, particle effect, items, powerups, platforms, randomize, spawn a random object, spawn object on timer, spawn, objects, anything, unity spawn, unity instantiate, unity spawn objects, how to spawn objects in unity, unity spawn tutorial, unity spawn items
Id: gsU7mZv3TtI
Channel Id: undefined
Length: 10min 45sec (645 seconds)
Published: Sun Aug 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.