Shooters Detect Attackers - Questions?

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 :slight_smile: 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.

The support has been overwhelming :rofl:.

Hi Vaughan,

On of the things which will make it difficult for people to respond is if your project has detoured slightly from the course one, even little things, for example “LaneSpawners” in Step 1. When I look at the project for the completed section there is a script called “Spawner.cs”, is this what you are referring to? I would guess so, but I’m not 100% certain. If I respond on what “Spawner.cs” is doing this could be entirely different from whatever _“LaneSpawner” is in your project, if you get my drift.

If we are talking about the Shooter.cs script, then you are correct, unless it is attached to a GameObject it isn’t going to be doing anything, and thus if the defender hasn’t been placed (instantiated) in the scene, it isn’t attached to a GameObject.

Step 3 -

    /// <summary>
    /// Sets the attacking spawner for this lane
    /// </summary>
    private void SetMyLaneSpawner()
    {
        Spawner[] spawnerArray = GameObject.FindObjectsOfType<Spawner>();

        foreach (Spawner spawner in spawnerArray)
        {
            if (spawner.transform.position.y == transform.position.y)
            {
                myLaneSpawner = spawner;
                return;
            }
        }

        Debug.LogError(name + " can't find spawner in lane");
    }

The code above is from the completed section, but probably resembles what you are looking at.

To answer your question, if the Shooter.cs script is attached to a defender as a component, then yes, each defender which is placed on the play space is going to run this code - but only once, when the Start method is executed and subsequently called the SetMyLaneSpawner method. It is trying to establish which spawner is for the lane which the defender has been placed in.

Step 4

I’m a little confused by your question on this one. Are you saying that you have only placed one defender, yet projectiles are being created on more than the one lane where that one defender was placed? Or, have you placed more than one defender?

If you have placed more than one defender, then the Shooter.cs script is a component of each of those defenders. Each defender will execute the Start method and subsequently the SetMyLaneSpawner method, so each defender knows which lane it is in. From each of these defenders, the Update method is called and the check to see if there is an attacker in its lane (ahead in its lane) is performed. If there is, then it should start shooting projectiles.

1 Like

Woah! So, you are saying each and every defender get a LaneSpawner object gets attached to the defender regardless of another object being in the lane? If so, that was NOT how I was interpreting the code. It’s starting to make more sense now. I could tell from the shooting code that it was iterating through the 5 lanes, I just didn’t know HOW it was doing that.

It’s just not something I would ever have done.

I still have to let that sink in - because that was my whole problem with this game!!!

Each and every defender gets a reference to the LaneSpawner in it’s lane.

You could think of it as having a team of defenders in a lane, but they are a team of non-team-playing members, they don’t talk or share. As such, each one greedily says “That’s my lane spawner”, as opposed to perhaps the first defender who gets placed in the lane saying “This is our lane spawner” and sharing that information with every other defender spawned in that lane.

It isn’t perhaps the most efficient approach, but, you are only dealing with a set number of lanes, and a maximum number of defenders, even if you put one in every space in every lane, there are not that many. Once each lane spawner is found for the defender, it jumps out of that foreach statement straight away, so the defenders placed in the first lane (lowest number) will be found more quickly than those in the fifth lane, which would perform four iterations before finding their lane spawner.

Hope the above helps :slight_smile:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms