Unity3d Creating a Timed Spawner

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello coders this is Jared with Renaissance coders if you want to see more videos like this one hit that like button and subscribe to the channel the Fancy YouTube algorithms will rank us higher and enable us to keep making great content for you thank you for your time now let's get into the video okay coders in this video we are going to add an additional spawner to our scene so let's take a quick look at the current scene that we have if I just press the play button here we can see that we've got uh a pivoting or just sort of a wobbling platform I can click and instantiate a whole bunch of objects and they just sort of collide with it and bounce all over the place so we've got a pretty cool scene going so far now for this video we won't really need to update our level a whole lot but we are going to create another quick script now this new quick script is going to be a timed spawn script that instantiates an object over time with the delay in between instantiations so let's go to our scripts folder which I already have open here and we're going to create a new C script and we can just call this timed spawn now let's open up this script and get to work okay so I've got my script open here and as per the usual we are just going to set up our variables first so we're going to say we need a public game object and we're going to call this spaw we're going to need a public bull stop spawning set it equal to false we're going to need a public float forour spawn time and we're going to need a public float forour spawn delay okay so those are the variables that we're actually going to need for this script now usually this is at the point at which I delete the start function but we're not going to get rid of the start function in this script we're actually going to get rid of the update function I know shocking right it's like how is this going to work without the update function well we're about to find out so we're actually going to create a new method here we're going to say public void spawn object okay and inside of this script we're going to instantiate we're going to instantiate our spawn e at our transform. position and our transform. rotation okay and then we also want to say if stop spawning then we want to do something okay so what do we want to do well if stop spawning is true then basically what we're going to do is stop calling our spawn object but of course we're not calling our spawn object yet so what we're going to do up inside of our start function is is create an invoke repeating call which is something that Unity uses to call a method over and over with an initial time and then a delay after that so let's just write this out and we're going to say invoke repeating and inside we're going to write out our method name and then we're going to put in our spawn time and our spawn DeLay So this is going to call spawn object and this is the initial time it takes to actually spawn and this is the the delay in between instantiations after that so inside of our if stops spawning we just want to call cancel invoke spawn object okay so this is a pretty simple script that's all there is to this so now we're going to go ahead and save that we're going to go back to our level and we actually have to make a couple of level updates here so we already have several spawners set up so if I zoom up you can see that we've got one two 3 four five actually set up so I'm going to go ahead and select all five of these and I'm going to add components and add the time spawn script to it now I want to actually stagger the start time and the delay time for each of these so I'm going to select one at a time and I'm actually going to go ahead and do set up each one with a game object and all of the other variables that we need so the first one will go uh you can get the green our spawn time will be one and our spawn delay will B3 the next one we're going to drop in our yellow our spawn time can be 1.5 and our spawn delay can be let's go with four and on to the next one red spawn time one spawn delay let's go with four again spawn spawner three or four let's drop in our blue and now our spawn time we're going to go with one again in but this time we're going to set it at 2.5 and now for our last one we're going to drop our black prefab in and our spawn time is going to be two with a spawn delay of three okay now I do want to change a few other things really quickly because this will be kind of hectic um to look at so what we're going to do is we're going to remove our Rotator script from our platform we're going to go back to our main camera and let's add back in the orbit and the change camera tar targets now if you're following along um and you haven't seen these other videos I'll be sure to link to the playlist and a couple of the other videos in the description so that you um you know can get to this point in your own script and these aren't necessary to test a timed spawner it's just sort of you know making the level look a little cooler so let's go ahead and press the play button and test it out okay so obviously we we are spawning so here we go you can see that they're actually spawning and moving around we've still got our wind pushing the objects off um I don't really like how the uh Orbit's affecting this so I'm going to take off the orbit and now we'll just have the camera change targets so that we can see the actual spawning taking place we can see that the the uh red and black ones were spawned at just about the same time but now they're staggered so this is something you can use to just sort of uh instantiate objects at different times and things like that which is really cool and it's a very very simple script and you may have noticed I didn't set in a condition to actually set uh stop spawning equal to true but you could come in and just click stop spawning if you wanted to just to make sure it's going to actually stop the spawning so if I turn all of these off then they now yep so now we are done that was all of them are now turned off okay so that's definitely pretty cool you know the fact that we can get some spawn or some time spawning occurring that quickly that easily is really really awesome especially you know with um you know a lot of games now you have sort of this uh element of randomization occurring so it's really helpful that we can create something like this this quickly of course we do have a couple of videos like I said said before in our playlist that can help you to randomize this spawner I'll be sure to link to the playlist and the other videos in the description like I said before so definitely check those out they're really great videos they're they're not that long so they're definitely worth a watch if you do enjoy these videos be sure to like And subscribe as always thanks for watching
Info
Channel: Renaissance Coders
Views: 79,177
Rating: undefined out of 5
Keywords: unity, unity 3d, timed spawner, c# timer, unity instantiate, instantiate game objects, unity C#, C# scripting, game development, unity scripting, unity prefab, unity spawn, unity spwaner, unity3d, timer spawning unity, unity spawner, scripting, c#, unity invoke repeating, unity spawn object on timer, unity simple spawn, unity instantiate object on timer, unity instantiate invoke repeating, unity game development, unity functions, unity simple scripts
Id: 1h2yStilBWU
Channel Id: undefined
Length: 8min 19sec (499 seconds)
Published: Sat Jun 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.