Spawning Objects in Unity [Using Instantiate]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's easy to click and drag objects onto your scene but what if you wanted to have objects continuously load to create an endless run or style game in this video I'm going to show you how easy it is to spawn obstacles automatically onto your scene [Music] if you have not watched my mobile joystick tutorial yet go ahead and watch that video first because we will be using the script and assets from that video for this tutorial the link for that is in the description now if we go ahead and open up our project we should have a spaceship moving through space that we can move by clicking and dragging our mouse and in Photoshop I crudely drew an asteroid for our object to avoid so let's go ahead and import this into our scene and the first thing we want to do is add a circle Collider to our asteroid when it comes to enemies I like to shrink the hitbox on these colliders to give players a little room to avoid these obstacles it tends to give gameplay a much smoother and better experience and since we will be dealing with collisions we don't want to use translate to move our asteroid instead we want to use velocity so we will need to go ahead and add a rigidbody 2d to our asteroid and then let's just go ahead and set our gravity scale equal to zero next let's add a script to this object and let's just call it asteroid the first thing we want to do is set up a reference for speed so that we can tell our scene how fast we like our asteroids to move so let's create a public float called speed and let's just set the default to ten then let's create a private reference for our rigidbody2d and let's just call this one RB then in our start function let's find our rigidbody 2d and set it to our RB reference by using this dot getcomponent rigidbody 2d and an easy way to move our asteroid from right to left is to set our rigid bodies velocity to have a negative x value so let's create a new vector to and for our first parameter let's set it to be negative speed and for our Y parameter let's just set it to zero because we don't want it to move on the y axis at all this will move our asteroid from right to left and a constant rate based on our speed value then go ahead and save the script and go back into you and any and press play we should see that our asteroid flies off the left side of the screen and if we were to duplicate the asteroid a few times and move them a randomly around the screen we should see that they all move together along the x axis although there's a problem here even after these asteroids go off the screen they remain in the scene this could be problematic as time goes on and we have thousands of asteroids floating through space having too many objects on the scene could drop our frame rate and even cause our game to crash so instead we want to adjust our script to remove objects from our scene once they fly off the screen we can do this easily with our screen bounds calculation that we use in our screen bounds tutorial video so back in our script up at the top let's create a private vector to reference for our screen bounds calculation then in our start function let's define our screen bounds variable I'm just going to paste this line in but basically what it does is is it defines the boundaries of our screen on an x and y axis since we are using screen to world point our screen bounds that X will be a negative value so our in our update function we just need to check to see if our astroids x position is less than screen bounds not X which is equivalent to saying is it to the left of the screen we can do this by writing if transform position X is less than screen bounds X if it is let's put destroy this dot game object this line basically removes our game object from the scene then if we go back into unity we should see that once our asteroid reaches the limit of the screen it is removed but since it's removed the minute it touches the left side visually the asteroids appear to vanish before they even leave the screen to avoid this I usually just pad the screen bounds or to keep it simple you can just do screen bounds X x 2 now this is cool but it requires us to manually add asteroids to the scene instead we want to load new asteroids automatically and we can do this very easily by creating a prefab of our asteroid object and using instantiate to spawn it so first thing we want to do is grab our asteroid game object and drag it into our project window this creates a prefab of the object basically a prefab is a game object containing all of its elements this includes the rigidbody the circle Collider and the script that makes it move then we need to create a script to deploy our asteroids on the screen we can create an empty game object to hold this script but for now let's just add it to the camera object and let's call this new script deploy asteroids and let's go ahead and open it the first thing we want to do is create a reference for our asteroid prefab we can do this by creating a new public game object called asteroid prefab and then below it let's create a new float for spawn time and let's just set it to one for now this will be used to tell our function how often we want to spawn asteroids next let's create a new function to spawn our asteroids let's just call this spawn enemy then we're going to use instantiate to load our prefab onto the scene so let's create a new game object and let's just use a as a reference then let's set it to instantiate and let's put our asteroid prefab as the parameter so it knows what to clone and then since we want to clone this as a game object let's put as game object at the end in this one line we just edit our asteroid to our scene now we can use our reference of a to manipulate it since our object will move from right to left we're going to want to place this object off to the right of the screen in order to do this we will need to calculate our screen boundaries again so let's copy and paste that from our previous script including the reference from the top then let's modify our asteroids position by setting its transformed opposition for the value let's create a new vector2 and set the x coordinate to screen bounds x times negative two we use negative two to place the asteroid way off the screen on the right then we want our asteroid to randomly be placed along the y axis to give us something to constantly move to avoid to do this we will use random dot range and for the minimum value let's use negative screen bounds that y and for the maximum parameter let's use a positive screen bounds that y value now the only thing we have left to do is set up a way for us to call this spawn enemy function every one second for this we will use a quarantine quarantines can look scary but they're actually rather simple I plan to discuss in depth how they work in a future video but for now just follow along with what I do let's start with a normal function and let's just call it asteroid wave then inside it let's just call our spawn enemy function then in order to make this a quarantine we need to add ie numerator in front of our function and then back at the top we need to call our quarantine by using start quarantine and passing our new function as the parameter and then lastly to get our new function to loop every one second we need to wrap our function in a loop for this we will just use while true but you can replace this with a boolean instead maybe a boolean that can be changed to true when you're ready for this game to start and then the most important line we write yield return new waitforseconds and let's use our respawn time reference to declare how long we like to wait in this case is just one second now if we save the script and go back into unity we just need to drag our asteroid prefab into our inspector so our script knows what object we wanted to clone and then if we press play we should see that our asteroids prefab is automatically clone onto our scene and there you have it in an upcoming video we will discuss collisions and how to detect if the spaceship collided with one of these asteroids so make sure to subscribe to this channel and ring the bell so that you don't miss a future video [Music] [Music]
Info
Channel: Press Start
Views: 236,854
Rating: undefined out of 5
Keywords: spawning objects unity, unity spawn object at position, unity spawn prefab c#, unity spawn objects randomly, random spawn unity 2d, unity spawn enemy, unity spawn object at random position, unity instantiate prefab c#, unity instantiate prefab at position, unity prefab from assets, unity instantiate 2d, unity prefab tutorial, unity create prefab at runtime, spawn prefab unity, brackeys, waldo
Id: E7gmylDS1C4
Channel Id: undefined
Length: 10min 23sec (623 seconds)
Published: Tue Sep 25 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.