as i've just said we've just created one enemy. what we didn't want to force you guys to do was to repeat those steps again to make a fairly identical enemy. instead what we want to do is show you how you can use some of the things that you've just done such as the animator controller, for example, and reuse that
on a new enemy. so what we did was we have very kindly gone and created for you the zombear. a new enemy who is otherwise pretty identical to the zombunny. he looks like this. what we're going to do is to assign certain things to that zombear to make him work very much
in the same way as the zombunny. so the great thing about the unity animator system is that it's reusable, so these are states that have animation clips assigned to them. so when we look at the animator for our enemyac, there it is, we have a bunch of different states. we have move, idle and death.
now these states aren't the clips themselves, they are holders for clips. so those animations are setup in there and if you've got something that has a similar skeleton, like our zombunny and zombear do, we can reuse the state machine and reuse both the script that talks to these parameters and the parameters themselves. in order to drive a different enemy.
so in the prefabs, with my zombear selected, what i'm going to do is click on my animation folder and then drag and drop enemyac on to the animator controller property. so i'm dropping it on to this. currently it says none. so we made enemyac earlier for the other character, we're reusing it here
so it will work in exactly the same way. you will also notice it's got the same scripts applied to it, it's got enemyattack, health and movement, which are looking for this animator controller and looking for the parameters that we setup. so that's just going to work right off the bat. then the third enemy is our hellephant. he rather shockingly looks like this.
the difference with this guy is that he's not the same type of enemy. he's a different shape and his rig's different and his animations are different. however he does the same things. he has move, he idles and he has death. but they're not the same animation clips. so one way to approach this would be to make a brand new animator controller.
make the transitions. make new parameters for it and set it up in a fairly similar way. also in the operating system you've got copy/paste to create a copy of it. again it's redundant and completely unnecessary. yeah, so, we don't want to do this. so there's the new feature, fairly recently added called animator override controllers.
what this means is you can create lots of different classes of characters in your game. and if, for example, they all share a similar skeleton you can have the same state machine driving all of them but just change the animation clip that it uses. so we're going to create that asset. so if you click on the animation
folder in your project. then go to create you will see something called animator override controller. click on this and we're going to call this hellephantaoc, animator override controller. you'll notice that the icon is a little different from the others, the others have three squares and a play icon, this has a + icon.
because it's effectively taking the same state machine and adding over the top the ability to use different clips within the states. so one example for this could be that you have a character that runs a certain way and jumps a certain way but when he shoots or when he attacks it's totally different. so you use the same animator controller to drive
the running and the jumping etcetera but then you just override the attack. so we're going to override all three simply because we're reusing the same parameters and the same transitions. the way that we use this is that we select the asset in the project panel like it is now and we give it a controller to relate to, so we drag and drop enemyac
on to the controller slot at the top of the inspector. so what this does is it goes through and says 'what states does this animator controller have?' and it's found move, idle and death. then we have the ability to fill in new animation clips to replace the ones that are in those existing states so the animations, just like before, are within the character folder.
just a word of warning here, what we don't want to do is click off hellephant. so i'm going to click on characters and i'm going to click on the arrow for hellephant, i'm not going to select it because we don't want the inspector to start showing something else. we could lock the inspector but i'm not going to do that. so i'm going to go and drag move on to the move slot.
and i'm going to drag idle on to idle and death on to death. so effectively we've got the same states, the same state machine, the same logic for how he's going to behave, but the animation clips within each state are being overridden or replaced by these animation clips from the model. so the animation for that character
was done within that model. the rig doesn't have to be shared, but we can replace the animation with whatever we need. so the last step then is to go to our prefab of the hellephant with all of the scripts setup and just instead of dragging on the enemyac we're going to drag on the override controller
in to the animator slot. hellephantaoc in there. so now we have three enemies that are setup and ready to be spawned in to our game. we need some kind of manager object. so much like our scoremanager or anything else we want to have an individual object that is in charge of the
spawning of the enemies, how frequent it is, etcetera. so we like to keep things neat here so we're going to create a brand new object to manage this on. you could, like any script you can attach this to any object you want to. you could attach it to the environment, you could attach it to the camera or anything you expect
to stay in the game, but we're not going to, we're going to create an empty game object. there's not really any overhead or any problem doing it this way and it allows us to keep things modular and separate. the great thing about this of course is if we wanted to make this a prefab we could drop this in to another level and reuse it. so i'm going to go to the hierarchy this time
and go to create - empty and create an empty game object. i'm going to rename this enemymanager. it's a good habit to get in to when creating a new game object, empty game object, whatever, to ensure that it's at (0, 0, 0) it's at the origin, just in case you might be using it as a container for other things
and you don't want it to change the co-ordinates of child objects. but it's one of those things that might save you some confusion later when you're wondering why things are moving around and and that's why. so just something to keep in mind, it's a good habit to do. so i've reset the position to (0, 0, 0). then, very kindly we've prepared a script that will do this for you. in the scripts folder under managers
you will find enemymanager and you can simply drag and drop onto the enemymanager. so just to show you one other way of assigning a script. you can either drop it on to the name in the hierarchy, you can also drop it on to empty space in the list of components in the inspector. so i'm going to double click the name of that script to open it. this is quite a simple script this one.
it's got a bunch of public variables that need assigning so because this script is something we're going to use several times and assign different enemies, different spawn points to we're not going to want to use the script to find those, we're going to want to use the editor to drag and drop all of these things. that's exactly what we're going to do, so you see a lot of public variables up front.
the first thing if a reference to the playerhealth. we're going to want to check we're only going to spawn more enemies if the playerhealth is more than 0. then we need a reference to the game object of the enemy that we want to spawn. so those prefabs are effectively game objects that are stored as an asset.
so we have a reference to the particular one we want to spawn. then we have a spawn time, which is 3 seconds and obviously because it's public we'll be tweaking this, changing it to balance the game. and then we have a public transform array, so you'll note these 2 open closed square brackets called spawnpoints. we're not going to use that in this example. we're going to use one spawn point per
instance of this manager. but if you wanted to you could have a whole bunch of spawn points, and you can take this project away and create more spawn points and try it out and have fun with it. so we just wanted to give you that option. so when the game starts we use a function called invokerepeating and what that does is it basically means
you don't need to have a timer effectively for something that's going to repeat. >from the very start of the game we call the spawn function, so we name it as a string, and we have an amount of time to wait before doing it and we have an amount of time to wait before repeating it. we've just reused spawntime, we could set that to 0 but we've just reused it
so there's a bit of a breathing space at the start of the game. so invokerepeating will just repeat this every, well, 3 seconds. and we're checking if the player has some health and if he doesn't we return but if he does, if he's still alive and kicking then we are going to instantiate our enemy in to the game. so we have this integer called spawnpointindex.
now when you create an array, a list of things, an integer is there to represent the point in the array. so as mike mentioned earlier, any list of things, any time a computer counts it starts at 0. so this is the index, the index is the number in the list as an integer. so we want to pick a particular spawn point
and spawn from that point. this way if you do choose to use an array, a number of different spawn points then it will pick a random one to spawn at, making the game a bit more lively. so the way we pick a random one, is we say random.range. we start at 0 and we pick any number up to the length, so the final number of spawn points. it will pick any of those spawn points.
it can help to think of an array as an accordion folder, if you know what that is, a folder with a bunch of slots in it. it's still really one folder, that's what an array is, it's just one variable, but if you open it you've got a whole bunch of slots inside. and that's effectively how an array behaves, you can look at the first slot, the second, the third, the fourth and you can put stuff in there.
so an array of these spawn points is just a single object, a single array of those spawn points, if you open it up you will see all of the various ones inside of it. and that's what we're doing, we're just picking a random one pulling that out and saying 'that's our spawn point'. and we're reusing that information so we're saving that as a number so we're picking a number and saving in to that variable.
and the reason we do that is because of this instantiate line. instantiate just means create an instance of something, so spawns effectively is another word for it. and it has 3 properties, the thing to spawn, where to spawn it and what rotation it should have when it's created. so the first one is obviously the enemy, the one that we assigned to our public variable and we'll do that shortly.
and then the second one is using that spawnpoints array and then we're parsing in to the square brackets which point in the array. so if i was to parse in 0 it would choose the first transform that we drag and drop in to our list. but we're going to allow it to use the random one that it's just picked so every time spawn runs it will pick
a random number within that list of different spawn points, parse it in to the position and rotation. so remember that this is a transform and every transform has a position and a rotation. but that is very simply our enemymanager. i'm going to switch back to unity now. so we have this enemymanager game object now and we have this enemymanager script on it and as was hinted at
this enemymanager script was built to be modular, which means we can put stuff on, we can take stuff off we can have a bunch of spawn points, kind of future proofing this game so you guys can keep adding to it and stuff like that. before we start putting in the enemies and get them spawning we have to generate these spawn points where the enemies are going to appear at.
so the first thing we're going to do is setup a spawn point for our zombunny. and also if you can still see the animator window here we're just going to click back over to scene view because we don't really need the animator view anymore. what i'm going to do is create an empty by clicking the create drop down in the hierarchy view and this empty game object is going to be my zombunny spawn point.
as such i'm going to name it zombunnyspawnpoint and we can see it where it kind of exists right now but that's not where we want it to go. and so what we want to do is place this where we want the zombunnies to come from. now as was hinted previously we could have a bunch of different spawn points but we'll only have one per enemy for now.
as we move around in our scene we can see the spawn points where it is right now with these arrows to denote where it is in the scene, but we can also give a customised icon to this object so it becomes much easier to see it in the scene view and it's really easy to do so i'm going to ensure that zombunnyspawnpoint is selected
in my hierarchy and then i'm going to look at the top of the inspector and see this little colored cube right here at the top of the inspector, and if i click that i get a drop down that gives me all these sorts of options and using this i can pick an icon. now the zombunny is blue, i'll pick blue so they're color coordinated. now that i've selected that
if i look back in my scene view i'm going to see that zombunnyspawnpoint has a name with this icon around it. so it's very easy now to see exactly where that is, it stands out. i'm going to now position it. i'm going to supply the coordinates and rotation to position these, this is one of those things where it's a really good idea to supply the exact
values that we provide only because these spawn points are kind of behind bits of geometry inside a mouse hole, stuff like that, so if you're off a little bit your enemies are going to start spawning in walls and they're not going to because to come out and it'll be a problem, especially in the mouse hole, which has a very narrow opening so it's very important
to get these numbers exactly as we have them. so for the zombunnyspawnpoint we're going to do the position of x to be -20.5. we're going to have y be 0, and the z value is going to be 12.5. we are then going to provide a rotation which is 0 in the x axis 130 in the y axis and 0 in the z axis.
now we have the zombunnyspawnpoint and it's where we want it and if i look in the scene view there it is, i can see it, it's much easier to see now because it has that blue icon. so let's create our zombearspawnpoint. now again i can right click create new and stuff like that but in this instance what i'm going to do is duplicate the one i've already made.
and so i can click on the zombunnyspawnpoint in the scene and i can right click and select duplicate or i can simply click on it in the hierarchy and hit command-d on a mac or control-d on a pc. and that's going to create an exact duplicate. same name, same icon, same position, same everything.
now we have this as a duplicate, so let's go ahead and change it slightly. i'm going to rename it from zombunnyspawnpoint to zombearspawnpoint. and i'm going to change it's gizmo, again using the drop down in the inspector i'm going to change it from a blue oval to a sort of fuchsia oval since the zombear is kind of pink.
and then i'm going to change it's position and it's rotation. again it's fairly important to have these accurate. mine is going to be 22.5 in the x axis, 0 in the y axis and 15 in the z axis. the rotation of this is going to be 0 in the x axis, 240 in the y axis and 0 in the z axis. now we've created 2 let's go ahead and create the last one, we're going to do the hellephant. and the steps are going to be very similar.
so with zombear selected again i'm just going to press control-d or command-d or right click and select duplicate, just make a duplicate of that object. and we are going to rename it from zombearspawnpoint to be hellephantspawnpoint. once created, again in the inspector i'm going to click the gizmo and this time i'm going to choose
a yellow gizmo because the hellephant has that yellow stripe to it. and then i'm going to set it's position to be 0 in the x axis, 0 in the y axis, 32 in the z axis. i will then set it's rotation to be 0 in the x axis, 230 in the y axis and 0 in the z axis. and so if we now look at our scene view in a zoomed out way we can see all three of our spawn points.
we can see exactly where our enemies are going to be coming from once we have the enemymanager able to start spawning those. so we're just going to setup the first three instances of the enemymanager to spawn our zombunny. so if you click back on your enemymanager you will see your enemymanager script and that's setup there ready to go but we already
have things to fill in. the first thing we need to fill in is playerhealth, again we don't want our enemymanager to spawn enemies when the player is dead and so what we're going to do is locate the player and we're going to click and drag on to the playerhealth property of the enemymanager and that's going to locate the playerhealth script so that it only spawns at
the appropriate times. then we have this next section for enemy. this is the enemy that's going to spawn, but if i look in my scene i don't have any enemies, there are none, do we need to specify what enemy we're going to spawn, we don't have any objects here. luckily as will has already mentioned, because he's classy like that,
is prefabs are in fact game objects so if we look down in the prefabs folder we will notice our enemies, we'll notice our zombunny and our zombear and our hellephant are there and they are prefabs. they don't exist in the scene but they are still game objects and as such we can click and drag them in to the enemy property
on our enemymanager script and now it knows that it's going to instantiate or create copies of that zombunny prefab. now we can see spawntime is 3, what that means is when our game starts it's going to wait 3 seconds and then spawn a zombunny. and then it's going to wait 3 more seconds and spawn a zombunny and so on and so forth. over and over and over again.
but it still doesn't know where to spawn those. we have the script, we have the player, we have the enemy, we have the time, but if we actually expand this spawnpoints we will see size 0. remember, this is an array, it's a collection, we haven't given it anything yet. it's size is 0, there's nothing in here. now there's a lot of manual ways i can add to this but there's actually a really simple way.
what i'm going to do is ensure that spawnpoints is actually collapsed, and then i'm simply going to locate zombunnyspawnpoint and click and drag it on to the name spawn points. not down here anywhere, you will see on my mouse if you look at my screen when i click and drag and i move over the spawnpoints i get this great + icon if i let go now it's added to
this collection. if i had a bunch of spawn points i can click and drag each on their the name and it will automatically add them to 'the collection', so at this point if i run this he will start spawning zombunnies. so with that done we are going to save our scene and press play.
so we've put one in of the enemymanager. we designed this script to be modular so that you could reuse. we could have written a script that has arrays of different enemies and arrays for different spawn points, we just made this nice and simple. what that means is you can drag on more than one copy of this script on to your
enemymanager and assign the other game objects. so what i'm going to do is go back to the scripts manager folder and drag on a another copy of enemy manager. so i can just drag in to that empty space and let go. then as before i need to assign my player to playerhealth, and i need to assign an enemy so this time i'm going to choose from prefabs zombear.
and i'm going to drag my zombear spawn point and drop it on to the spawn points array, which will populate it. finally, one more time, scripts managers enemy manager. drop it on again. third and final time. and we're going to assign the player to
get the playerhealth script. we're going to go in to our prefabs folder. drag and drop the hellephant game object. this time we'll set a different spawn time to 10. and we're going to drag our hellephant spawn point on to the spawn points array so i'm going to leave that open like that so we've effectively given it 3 copies of the same script
we've assigned different enemies to spawn and different spawn points to spawn them from. and we can choose different spawn times, so if we want a spawn time of 10 for the hellephant we get fewer of them, so it's kind of like a tank kind of class, if you've played left for dead. so go ahead and save, and play. and you should be able to play a 3 enemy-based game.
remember that we put different spawn points at different places. the zombunny is coming from behind the dolls house. the zombear is coming out of the mouse hole. and the hellephant's are coming from behind the chest of draws in the upper corner of the game.
and your score should be working and if they manage to corner you you should be loosing health. we really should have made you lose health if you stepped on the legos because that hurts so much. yup. so that was the end of phase 9 and we have 1 more phase to go. subtitles by the amara.org community
0 Komentar untuk "winter road"