‫Welcome back.
‫I hope you're up to speed on some fruit's healthy fruits.
‫Let's go.
‫So what?
‫Our goal for this video is to spawn some fruits which will be spawned from the bottom and will shoot
‫up into our game scene or into our game view and then we can slice them.
‫So far we can't slice them, but we will be able to do so later on and we want to do that randomly.
‫So we want to create a random fruit and with a random distance in time.
‫So it shouldn't be created after exactly the same amount of time.
‫So let's create a new script for that and let's call that spanner, because that's what it's going to
‫be.
‫It's going to be a spanner.
‫So let's open that up.
‫And here we won't need the update method, but we will need several variables.
‫So for one, we'll need a game object.
‫So public game object and that one will be the fruit to spawn.
‫So the fruit that we want to spawn, it could be an orange, it could be a banana and so forth.
‫And we want to know which fruit we should spawn.
‫Then we want to know how much time we want to have between the different fruits.
‫So it should be, let's say, between 3.3 and 1 seconds.
‫So let's create a float min weight and that would be 0.3 seconds and the public float max weight.
‫So the maximum amount of time that we need to wait for a fruit should be 1/2.
‫And if we do it that way, we can easily add that within unity.
‫So that will allow us to do that.
‫Now within the START method, I want to start a routine which will constantly create fruits after well
‫this particular time that we have here.
‫And in order to do that, we need to use the start code routine method, start code routine.
‫And as you can see here, the START code routine needs an AI enumerator.
‫In order to start anything, we could as well start a method or start a method with an object.
‫But what we really want to have is this one here.
‫The first overload which needs an AI enumerator.
‫So start code routine of course, now needs an enumerator.
‫So let's comment that out now and let's create that AI enumerator.
‫And in order to do so, we need to create a method which will return an AI enumerator and it's supports
‫a simple iteration over non generic collection.
‫So that will allow us to do is to do things well to call this method multiple times and a start code
‫routine and let's go ahead and spawn fruits.
‫That's how I'm going to call it and what I want to check here while true.
‫So as long as this code routine runs, I want to spawn fruits.
‫So what I want to do in here in the wild loop is to yield return.
‫So it's not a basic return statement that we have used yet.
‫It's a yield return which you can use for new wait 4 seconds.
‫So if you want to do something after an amount of time, you can use this enumerator with yield return.
‫So wait for a seconds and I want it to be a random value between our range of minimum weight and max
‫weight.
‫And now what I will do in here is simply debug the log and fruits, get more fruit, get spawned.
‫All right.
‫Let's say that script and actually we need to call spawn fruit in our code routine.
‫Of course.
‫So spawn fruits.
‫Now that all works out.
‫And let's save the script once again and let's go back to Unity.
‫All right, in here, let's create a new empty object.
‫And I'm just going to call that spanner.
‫And let's get rid of all the oranges that we had here.
‫The lead and that's Boehner object, of course, needs a component.
‫So let's search for a script called Boehner here.
‫And as you can see, we could change the min weight in max weight any time.
‫Now, the fruit to spawn is, of course, the spawn fruit or the fruit that we want to spawn.
‫And in our case, it's our orange.
‫So let's go to the sponsor and drag our orange in here fruit to spawn.
‫And now let's just start that.
‫So the only thing that will happen now is, of course, that it will go to our console and you can see
‫fruit get spawned five, six, seven, eight.
‫So constantly fruit are spawned.
‫So that's good.
‫But of course, we don't see much yet and we'll need to change that.
‫All right.
‫So we could spawn from the spawn here, which has a specific position, as you can see.
‫Well, it's here.
‫You can't see it very well.
‫So let's add a gizmo.
‫So here on the right hand side, just next to the name of the spanner, you can choose to add an a little
‫look for it.
‫So as you can see here, now I have this let's use this purple color here.
‫So this is going to be the spanner.
‫This is just a gizmo, which allows me to see where an object is, even though it's not visible in the
‫game.
‫So it doesn't have any Sprite or any animation or things like that.
‫No match, nothing like that.
‫And only has this little gizmo now which will show me where it is in my game view.
‫So this is currently where it is and.
‫Let's drag it down a little bit.
‫Actually, let's position it or let's reset it first.
‫So it's going to be at 000 and I'm just going to call that spawn or.
‫Or spawned plays.
‫So this is where it's going to spawn.
‫Now let's duplicate that spawn place and drag it to the side a little bit.
‫And let's duplicate that one again, once again, and drag that to the side as well.
‫So now we have those three pointers about the position.
‫Don't worry about that yet.
‫We will check it out later on.
‫We will play around with the right position and find what the perfect position for it is.
‫Now about the two spawn places which are on the side.
‫So spawn place too.
‫And in my case it's the first one.
‫In your case it could be different ones, but the ones here to decide should not simply shoot to the
‫top.
‫They should slightly shoot towards the center.
‫So we need to rotate them.
‫We could either rotate them this way, so I'm going to rotate it like that, or I could simply add the
‫value in here.
‫So I'm going to say rotate by 30 degrees and I'll spawn plus two.
‫In my case should have -30 for the Z value.
‫So now both will rotate a little bit.
‫All right.
‫So next.
‫So now let's go back to our script.
‫Now that we have this -- and in our spawn fruit, we will need to have the different spawn places.
‫The spawn should know about the spawn places.
‫So let's create a transform array, which is our spawn places.
‫And now within our spawn fruits, we don't only want to debug, but we also want to transform the position
‫of the fruit that is created.
‫So transform t is born places at the position of random range and it should be a random position between
‫zero and spawn places dot length.
‫So what that means is however many spawn places we create, we want to just assign the position of our
‫fruit that we created at a random position of those places.
‫So it means it will be either at the first spawn place, the second, the third and so forth.
‫So however many we assign, that's what we say here spawn places dot length.
‫That's the spawn place that we want to choose here.
‫So the index of it will be that now let's create the game object, which is our fruit, and let's instantiate
‫that
‫and we want to instantiate the fruit to spawn.
‫So the fruit that we want to spawn at the position of our T so off the transform variable that we have
‫just created here, t dot position
‫and DT dot rotation.
‫All right, let's go back to Unity and check that out.
‫So now it should those partners actually, we didn't assign those points yet and we don't have a sponsor
‫anymore, so let's change that.
‫Right.
‫So the one that I've deleted, it's created create empty spawn.
‫And now let's add the component script here.
‫And was a sponsor.
‫Now we need fruits to spawn.
‫And we're going to start off with the orange again, as I said.
‫And the next thing is we need the spawn places.
‫So we need to assign how many spawn places we have.
‫And for now we have three different spawn places.
‫And I'm just going to drag the different spawn places into my script here.
‫All right.
‫So now let's start the game and see what happens.
‫And as you can see now, we randomly get fruits created in our game.
‫Now what you don't see here in this view.
‫So let's deactivate, maximize on play.
‫Let's see what happens if we start it here.
‫As you can see, we create oranges and we're going to create many, many oranges because we never delete
‫them.
‫So far, we never get rid of those.
‫That's something that we'll have to take care of.
‫Of course.
‫And of course, also we need to change the direction to which they shoot.
‫So currently they just fall down and they don't have any force applied to them to to fly to the air.
‫So that's what we want to change.
‫So let's go ahead and do that.
‫Let's apply a little force to our fruits.
‫And how we can do that.
‫How can we do that while we get the component rigid body to DX of our fruit and then we apply a force.
‫So add force and we have two different overloads.
‫We are going to use the one here, the force and the mode.
‫So the force that we want to apply is transform up multiplied with ten.
‫So that's just a value that I've assigned here.
‫And then the force mode to.
‫DX So the way we want to force them up is going to be impulse.
‫So for small to be actually impulse like that.
‫All right.
‫Let's save the script and let's go back to Unity.
‫Let's see what happens.
‫And as you can see, they are shot up and then they hit each other and then fall down again.
‫Now, what they don't have is the rotation, so I don't like that.
‫So let's have a look at the different places.
‫Now of course our spawn places will need the spawn or script anymore.
‫So let's get rid of that remove component from our spawn places.
‫All right.
‫So let's remove the spawn here as well.
‫All right, now let's quickly fix the problem that we have unlimited of fruits, so at least that we
‫don't destroy the fruits after a while.
‫So let's do that.
‫So here we just say destroy fruit after 5 seconds.
‫So the game object that we instantiate here, we want to destroy it after 5 seconds.
‫So as soon as it was in the air and then it fell down.
‫So it's fine to destroy it.
‫So let's have a look here.
‫What happens is will spawn and then they will get destroyed after a couple of seconds.
‫So we have five, six, seven, eight.
‫And as you can see, the oranges get destroyed again.
‫That's good.
‫It still doesn't work that we rotate them.
‫So the force goes in towards the direction that we rotated the spawn places to.
‫So let's have a look at that.
‫And the missing rotation has to do with one thing.
‫So let's go back to our code as what we do here is we transform up something by ten and in this case
‫we transform our script up so that we add a force to the spinner itself.
‫What we do want, however, is to transform up our fruit.
‫We want to add the force from our T, so which comes from our transform position, from our spawn place.
‫So we don't want that to from our spawn or itself, but from our spawn place.
‫And our spawn place has a specific rotation.
‫So now let's start that again and see how our fruits are flying to the different directions.
‫And as you can see, they are flying in different directions.
‫And let's have a look.
‫I have played around with the values here, so this one should be 30, that one should be -30.
‫And we can use a spawn place in the center with zero.
‫So now let's go ahead and do that again.
‫And you will see that they will fly towards different directions and currently they can even touch each
‫other.
‫But we're going to take care of that later on as well.
‫Now, what we need to change is our spawn places position because currently it's they're all a little
‫bit too high.
‫So let's move all of them a little bit down to around minus five or actually we can enter minus five
‫here and now.
‫Next, I want to change the power with which they are shot.
‫So let's have a look how they are spawning currently.
‫So as you can see, they barely make it to the center.
‫So let's change that.
‫In order to do that, we need to change the transform up.
‫So now it's ten.
‫But what we could have is a random force with which they are shot.
‫And let's create two additional variables for that public float main force is, let's say 12 and public
‫float max force is, for example, 17.
‫So these are just values that I played around with.
‫And now let's multiply with random range min force and max force.
‫So now what it will do is it will give us a random value and we'll add the force randomly now between
‫12 and 17, and we can easily adjust that within our settings here.
‫So within our spawn we will be able to change that.
‫So you can see min force and max force.
‫So let's start that.
‫We can then check out directly whether that's good values or not.
‫As you can see, they fly up and maybe we could even go further here min force, let's say 15 and max
‫force 25 and then you will see that they will even fly off the screen completely and fall down again.
‫So maybe 12 and 17 are good values after all.
‫All right.
‫So that's it about the Spooners.
‫Actually, we can still slice our fruits, so let's spawn some and slice them.
‫As you can see, if we have multiple in the scene, we can still slice them.
‫But of course, we want to do that with our mouse and not with our keyboard.
‫Great.
‫So in the next video, we are going to have a look at the blade.
‫So we're going to create the blade.
‫Look.
‫See you there.