From the course: AR Development Techniques 04: Advanced Techniques

AR shooting game, part 2

From the course: AR Development Techniques 04: Advanced Techniques

Start my 1-month free trial

AR shooting game, part 2

- [Instructor] In this video, we'll write some code to spawn the balloon models at random points in the environment. and after random intervals of time. First of all, let's specify some points in the scene or the environment where the balloons will randomly spawn. We can denote these points with any object or even an empty object. I'll use small 3D cubes to denote those points, so right-click, 3D Object, Cube, reset it, and I'll make this 0.1 in all the directions. So here's our device camera at the origin, and we want to spawn the balloons around the camera, or basically around us, so we'll place the spawn points around the camera. Let's do that. I'll name this PT, short for point, and I'll move this 1.5 meters in front of the camera. So the camera is here and one of the spawn points is 1.5 meters in front of the camera. Let's duplicate this point and move this by 0.5. Duplicate it again, and let's make this -0.5. Duplicate, one and -1. Now we have five points, five spawn points, let's add some spawn points on the other side as well. So I'll just duplicate this, all these points and move them to -1.5. Now let's add some points here and here. Duplicate and make this -1. Duplicate -0.5, duplicate, zero, 0.5, and one. Let's move all these points that we created and place them at 1.5, and now let's duplicate these and place them here at -1.5. So now we have 20 spawn points around the camera, and these are the points where we will randomly spawn the balloon objects. We don't want to see these spawn points, the spawn cubes in our scene, we just want to see the balloons spawned at these points, so let's select all these points and uncheck the Mesh Renderer and Box Collider. So these points are still here, but they're not visible in the scene. Now let's write some code to spawn the balloon objects at these spawn points randomly and after random intervals of time. So go to the Assets folder, I'll create a new folder for our scripts, I'll create a new script, right-click, Create, C# Script, and I'll name this SpawnScript. Let's add this script in the scene, so create an object. I'll name this SpawnScript, and drag the script right here. Open the script. First, let's create two arrays, one for storing the spawn points, and the other for storing the balloon objects. This will be a public array, Transform spawnPoints, and the other one will be for balloons, GameObject, array, balloons. We don't need the Update function, so remove it, save the script, and go to Unity, and now let's fill these arrays. The total number of Spawn Points are 20, so let's make this 20 and the balloon objects are three, So let's make the Size three. Now, let's fill these. I'm going to fast forward this. And now let's add the balloon objects here. Now to spawn the balloon objects, we'll create a function and inside the function, we'll basically do two things. First, we'll wait for some time, and second, we'll spawn a balloon model at one of the spawn points from this array. So first we'll wait for some time, and then we'll spawn a balloon model at one of the spawn points, then we'll again, wait for some time and spawn a balloon model. This will go on and on, and this is how we'll spawn multiple balloon models in our environment, and we are going to randomize all the events. That is, we'll wait for some random time and then spawn a balloon model, and that balloon model will also be randomly selected from this array That is, it can either be blue, yellow, or red, and the point at which the balloon model will spawn will also be chosen randomly from the spawnPoints array. Let's actually do this, it will provide you more clarity. To wait for some time, we need to use a function called WaitForSeconds. To use that function, we have to create a coroutine. The structure of a coroutine is just like a function, so I'll create one. IEnumerator, and I'll name this StartSpawning, and we'll call this coroutine as soon as the app starts. So we'll call it in the Start function. Start Coroutine, and inside it, the name of the function. Now to wait for some time, we'll write yield, return, WaitForSeconds, and inside this, we'll write the number of seconds that we want to wait for. So if I write five, then the execution will stop at this line for five seconds, and after five seconds, the next line of code will be executed. As I said, we are going to randomize everything, so we are also going to randomize this time. So I'll write Random.Range 0.0F to 1.0F. The range function will produce random values between these two numbers, 0.0 and 1.0, so it can be 0.3, 0.4, 0.1, or anything between these two numbers. So the execution will stop at this line for some random time, and after that, we'll spawn a balloon model at one of the spawn points. So to spawn the balloon model, we have to Instantiate it, and this function will take three values, one is the object that we want to spawn, the position at which we want to spawn that object, and the rotation of the object. So the object that we want to spawn is a balloon, so we'll choose a balloon from this array. So I'll write balloons, and since there are three balloons in this array, I can write either zero, one, or two, but as I said, that we'lll randomize everything, we'll randomize the selection of balloons as well. So I'll create new variable which will be randomBalloonIndex, and I'll again call the range function, Random.Range, and it can be anything from zero to the length of the balloons array. Balloons.length. This value will be three because there are three balloons in the balloons array, so this function will return a random integer from zero to three. So it can be either zero, one, or two, and that will be stored in this variable. So now we can use this variable, the random index variable right here. So this will instantiate a random balloon object. So the balloon can be either yellow, blue, or red. The second value is the position at which the balloon will be spawned. So for the position, we have all these spawn points and we will spawn the balloon at any of these spawn points points randomly. So we'll write spawnPoints, and again we need a random index here, so I'll just copy this, and name this randomSpawnPointsIndex, and the range will be from zero to spawnPoints.Length, and this random index, copy it right here. So this will randomly choose a spawn point but we need the position of that spawn point, so we'll write .position, and the third value that we need is rotation. For that, we'll write Quaternion.identity. Now, what this will do is, once the coroutine is started we'll wait for some random amount of time, after which, we'll spawn a random balloon at a random position. Now this procedure will just spawn one single balloon, so to continuously spawn new balloons, we'll have to call this procedure again and again. So we'll call the same procedure, which is StartCoroutine, after the execution has ended. Now, this will go on and on, and multiple balloons will be spawned. Save the script and go to Unity, and now let's run the project in the Unity editor. So you can see the balloons spawn at random points after random intervals of time. Right now these balloons are spawning, but they are not moving up, so we need to add that functionality as well. Let's do that in the next video.

Contents