[Unity3D Hex Map Game Dev Tutorial] Part 15: Preparing for Multiple Different Unit Types

Hello Unity fans. At the end of our previous video, for which the link is in the top-right and in the description below, we we’re at a position where we had complete animation and action cycles for both woodcutters and stonemasons. This includes the functionality to force them to approach their cabins from the correct direction, to prevent them from walking through the walls. But our unit script could only handle one type of unit at a time. We had to choose whether we wanted woodcutters or stonemasons. Today, we will expand on our scripts such that any number of different resource gatherer unit types can be handled by one unit script.

Part0 (Introduction): https://youtu.be/92Gab40nufM
Part1 (Hexes to Water): https://youtu.be/2ktop35T2rI
Part2 (Features and Textures): https://youtu.be/V-iAaIYliBU
Part3 (Units, Visibility): https://youtu.be/ZfutE1LlTvA
Part4 (Random Maps): https://youtu.be/HpqD3m6VmVM
Part5 (Post Processing): https://youtu.be/ejbL2fNlaZk
Part6 (Resource Gathering): https://youtu.be/9ddQMTeRCa8
Part7 (Spinning Selector): https://youtu.be/0otHclifG9o
Part8 (Preparing for Resources): https://youtu.be/BsnlfoPkj5w
Part9(How Much Wood?): https://youtu.be/87800hX4idc
Part10(Dynamic Trees): https://youtu.be/uCulaS6H_bo
Part11(Meet The Meeples): https://youtu.be/sglozmOoPdE
Part12(Harvesting Wood): https://youtu.be/xwgK1BWWDjo
Part13(Harvesting Stone): https://youtu.be/LGtn9BIl1dU
Part14(Walking Around Wallls): https://youtu.be/ZO0N9VP5zEo

Up until now, our unit script had its various speed parameters, as well as resource type, hardcoded as constants, so we could only have one speed, and one gatherer type on our map. We now change these to public variables which we will populate when the unit gets created, and which will be dependent on the different characteristics of the different gatherer types. We also supplied it with only one array of unit prefabs, from which the unit model was chosen, so they all had to be of the same type – say all woodcutters. We had to change it manually when we switched from woodcutters to stonemasons. Of course, we’d like to have both types of units on the map at the same time, and still more unit types later on. So, we have to find a way to make the choice of unit type dynamic and allow the easy and seamless addition of extra unit types as we progress, without being dependent on hardcoded parameters.
We start off by defining an enum for gatherer type, like we did for resource type. We’ll only be looking at woodcutters and stonemasons in this video, but I’ve added on farmers, so these units at least have some prospect of having a decent meal one day. And, just as we had a class containing all the information or specifications for each resource type, we add a similar class for gatherer type. We link the gatherer type with the resource such a gatherer will harvest and cater for all the speed parameters that we had in the unit script. We include a counter that we’ll use to cycle through the different prefabs for the gatherer type when more than one is added to the map, so that we can have units with some variation. We include a constructor to initialise our variables when defining different gatherer types. Now, again similar to where we defined our different resources and their characteristics, we can define our different gatherer types and their characteristics. Note that we can specify different speeds and position buffers for different gatherer types, so let’s make the stonemasons slightly slower. Also, since their pick-axe animation lunges forward, where the woodcutter swings from the side, we also need it to stand slightly further back when breaking the rocks.
We can now define different gatherer type characteristics, but we still need to supply a list of prefabs for each gatherer type to use as visual models. I’ve created a prefab linker script, which I’ve added on to the HexGrid game object. It defines a class gatherer prefab, which contains the indication of which gatherer type it is, and a list of prefabs to use as visual models. We have a serialised instance of this called allGathererPrefabs, and we can populate this on the HexGrid game object for our different gatherer types. We only populate it for woodcutter and stonemason for now, and add the four prefabs for each type which we’ve been using all along.
All that remains is for us to adapt the create unit method to cater for different types. We previously spawned a unit by pressing u on the keyboard. We will now adjust this to spawn a woodcutter on pressing u and a stonemason on pressing i. We need to now pass a gatherer type to the create unit method, and it needs to do some extra work to determine which gatherer type specification entry it needs to use for the unit. It achieves this by looping through the prefab groups until it finds the one that equals the unit to be created, saving the position as index.

Post Author: hatefull