Shooters Detect Attackers - Questions
Sorry about the length of this but it couldn’t be helped. I need to be clear what I am saying and what I am asking about.
Right … I had to stop progress with the course at the Shooters Detect Attackers lecture, for a long time. First, because Unity wouldn’t run a script I had developed properly until I deleted everything from the playboard, I still can’t understand why. But I could reproduce the error, so, it was a Unity problem. So, a long detour there. So, struggling on, I managed to get the Shooters shooting at the Attacker, using the code:
defense.GetComponent<Animator>().SetBool("isAttacking", true);
after a couple of days of more struggling managed to get the shooters stopping after there was nothing in the lane using code like the above:
defense.GetComponent<Animator>().SetBool("isAttacking", false);
But I didn’t even attempt to get the shooters stopping attacking if there was an attacker behind them – mind was shattered by that point. Even worse if you put defender down on the same square as an attacker, but, after the attacker had passed though the centre of the square – the attacker was munching on nothing And I mean munching, I punched in a couple of sound FX to help with the atmosphere. But it also happened with @ben’s code as well.
So, I looked at the solution, it looks efficient does precisely what was intended, but, can I understand it … no! So, what this is, is a discussion on the code used as per the lecture, my problems with it and helping me to get to grips with what is going on. The only way I know to do that is trace out the program flow. And try and predict the results.
Step 1
Ok, run the program …
Prediction: it’s going to find out the LaneSpawners and report back the names, I have in the print statements – no!
The script does nothing – that was the first shock! Would I right in saying, that the reason it does nothing is because there is no instantiation of the “Shooting_Projectiles” scripts as there are no defenders yet available on the Playboard, which has the “Shooting_Projectiles” as one of the components?
Step 2
Next, click on the buttons, put on Gnome at position: 2,1 (Lane 2 column 1] now we have a shooting Projectiles script fully operational.
Prediction: it’s going to set the spawnerArray[], finds all the GameObject’s that have the <Spawn_Attacker> code on it – the 5 lanes. Yep, all good.
Step 3
Well ok, lets see what happens now; SetLaneSpawner().
Prediction: will return the highest y value object every single time – no, wrong.
Right, we are looping through the 5 lanes looking to see what lane has the same y value as the object we “just” put down. Huh - does this mean that a new version of the script runs on every click on the playboard?!? In the meantime, the second lane will fit that requirement. Now we set myLaneSpawner and break out of the loop.
Step 4
Now we are moving through to the Update loop.
And we run into the isAttackerAhead() function. So, we have just made a new Defender at position [1,1] on the playboard and myLaneSpawner is going to set to lane 1 and stay at that value for the remainder of the game unless we click out of it that lane.
Prediction: the code can only throw out projectiles from the last lane clicked on – no.
So, with myLaneSpawner staying at the same value how is it throwing out projectiles on 2 lanes? Mind is going into meltdown.
To anyone who can help me understand what is going on with the code here is a big thankyou in advance.