My version of isAttackerAheadInLane totally different but seemed work ok

private bool isAttackerAheadInLane() {

        if (myLaneSpawner.transform.position.y == transform.position.y)
        {
       return    myLaneSpawner.GetComponentsInChildren<Attackers>().Length > 0;
            
        }
        else {
             
            return false;
        }

    
    
    
}

This would always be true if you set myLaneSpawner correctly, because this is how you pick the correct spawner in SetMyLaneSpawner().

I’m not sure about the efficiency of this approach, but it would not cover when Attackers are behind Defenders, as it is not checking the Attacker x coordinate.

Ha that’s nothing have a look at what I came up with, when there was little direction from the course and left to my own devices.

I didn’t even use the ahead in lane because I couldn’t figure out how the use it. And forget about trying to stop shooting enemies behind you, that was never going to happen. And talk about inefficiency, the code ending up running Unity into the ground with about 4 attackers in each lane.

Still can’t follow the code in the solution, but, I’ve only been at it for 4 days, I’ve got plenty of time to try and work it out.

public void NewShootMethod()
{
//var to find y value of the lane with spawned obj
Vector2 lane_y;
Vector2 defend_y;

    foreach (Spawn_Attacker lane in spawnerArray)
    {
        lane_y = lane.GetComponent<Transform>().position;
        //print("Hello");
        {
            foreach (Defender defense in objToLoopThru)
            { 
                defend_y = defense.GetComponent<Transform>().position;
     
                if (lane.GetComponent<Transform>().childCount > 0 && lane_y.y == defend_y.y )
                {
                    //print("Enemy in lane: " + lane_y.y);
                    //if (lane_y.y == defend_y.y)
                    print("Enemy in lane!!!");
                    defense.GetComponent<Animator>().SetBool("isAttacking", true);
                }
                else if(lane.GetComponent<Transform>().childCount<=0 && lane_y.y == defend_y.y)
                {
                    print("lane is empty");
                    defense.GetComponent<Animator>().SetBool("isAttacking", false);
                }
            }
        }//end inner forech - defenders
    }//end outer foreach - lanes

}//end NewShootMethod*/

Privacy & Terms