Week 12 Blog – More Assignment
So it’s week 12 and I’ve got until the end of the week to finish my assignment, I think it’s safe to say it’s crunch time. Last week I talked about how I used coroutines to get a gather mechanic working in my assignment and how I will do a similar method for my combat units. So this week I’ll gloss over how I accomplished just that and how the combat coroutines differ from the gather one. I’ll also explain my method for building placement since I’ve got it working for the most part.
Currently I have the functions working for two combat units including a warrior and a mage. The warrior uses melee attacks and the mage fires lock on magic missiles. Just like the worker the coroutine is started from the selection manager script but instead of having the resource type parsed as a string, the combat units require a target parsed as a game object. The coroutine itself functions almost the same as the workers so I’ll just explain the differences. When the warrior reaches its target the coroutine will wait until the attack timer has reset before dealing damage.
For those who don’t know the attack timer is a simple concept that manages fire rate. To implement this you need two floats, I call one attackTimer and the other attackRate. The attack timer variable doesn’t need to be set to anything initially and the attack rate is the amount of seconds of delay you want between each attack. Before a unit attacks it will check if the attack time is less than Time.time. Because attack timer is not set then it will be less than the current time. After the attack the attack timer variable is then set to equal Time.time + attackRate. So basically if I attack at 20 seconds into the game and my fire rate is 2 seconds, then attack timer will be set to 22, the attack command will not activate until the current time reaches 22 seconds which is 2 seconds after the initial attack.
Moving on, the TakeDamage function is something that both the enemy units and the player units have. It’s just a function that subtracts damage from the units health, the damage is a float that is parsed when the unit deals damage. After the warrior deals damage the coroutine is restarted and the current target is parsed through again so it will attack the same target. Much like the worker coroutine there needed to be many if statements to make sure the target wasn’t null, but in this case instead of stopping the coroutine like with the worker, the combat unit will search for another target within a proximity.
Screenshot of the on enemy click coroutine in the warrior script
The FindClosestTarget function is an important feature to my game and helps with the automation of combat so that the player can have multiple processes running at any given time, this is why I used coroutines in the first place. I learnt this method earlier on in Scripting 1 and it took me a while to wrap my head around. This function returns a game object, at the start of the function a new array is created, in the case of the player units the array just gathers all the enemy units in the map. A new game object is made called target and set to null. Then a float called distance is set to the max range, sometimes this is set to infinity if you wish to check targets from any range. The function checks if the array length is larger than 0 and then for each enemy unit in the array it will set a new float which is called diff (difference between 2 the units) and it is set to the distance between the player unit and the enemy unit using Vector3.Distance. Then an if statement is made and checks if the diff is smaller than the max distance. If it is then the max distance is set to the diff and the target is set to the unit in question. So lets say we have 3 targets, target A is 10 metres away, target B is 8 metres away and target C is 15 metres away. I check if target A is closer than the max distance which in this case lets say its infinity. It is so this becomes my new target and max distance is set to 10 metres. Then I check if target B is closer than the new max distance which it is so now this is my new target and my max distance is set to 8 metres. Now we check target C and see if he is closer than the new max distance, he is not so he is ignored. As you can see this method returns target B which was the closest target.
Screenshot of the find closest target function in the player unit script
I have most of my functions completed for the game but I will be polishing up on animations, adding general game functions like pause menu and main menus and maybe even a new unit type before submission. I’m thinking along the lines of an archer that has non lock on projectiles.