[Question] Only One Defender PER Square

I am able to place more than one Defender on a square, which isn’t ideal. I don’t recall this being discussed in any of the lessons. Anyone else encounter this? And if so, what are some suggestions to remedying this issue? I have on in mind, which functions similar to how we determine when to trigger when an Attacker is in their lane. But, am curious to see what other suggestions are out there.

I had this happen to me, The reason it’s happening (at least in my investigation), Following the course, I set the Level canvas Render Mode to World Space and because of that everything was on one layer only so nothing was “blocking” your clicks also, I couldn’t change the Zed position to put it behind everything.

I fixed it by changing the Render to Screen Space - Camera and then moving the Core Game object to zed 1, so it sat behind everything, that way when you place your defenders, you would click on them and not the space behind it to place a unit. Try that. Also double check your colliders on your defenders and make sure they are covering the square.

I started to go down this route, and it borked my whole Level Canvas and Core Game…took me an hour just to get things back to normal.

I’m thinking of heading down the route of a programmatic solution. Since everything is working off the game-grid space, perhaps leveraging the Defender script to insure no other defender has an X && Y coordinate as the one I am about to place --> and if so, prevent the operation (placement).

1 Like

Something must be off in the Z coordinate of the colliders.

You should check if the Game Camera object is on a negative Z plane (i.e. transform.position.z == -10), the Level Canvas parent which contains the Game Core panel is on a positive Z plane (+5) and all the other game objects at Z=0 (attackers, defenders, buttons panel, etc.)

Another possibility is that, for some reason, your defender prefabs used at runtime to spawn on the play field are on the Ignore Raycast layer, if it’s so revert the layer to Default.

1 Like

@Galandil - correct. The colliders on the prefabs weren’t registering because the z position was off.

I do not have the issue for the ignore raycast set, but that’s an interesting item!

You’ll need that layer set on the projectiles, if you don’t want them to prevent the player from placing new defenders in the squares under them. :wink:

I had the same problem, and yes it was caused because my defenders were in the same “Z” as the Level Canvas. I decided not to change the Z position, but instead I created a method on my DefenderSpawner script:

bool IsPositionOccupied(Vector2 positionToCheck)
GameObject[] myDefenders;
myDefenders = GameObject.FindGameObjectsWithTag("Defender");
foreach (GameObject defender in myDefenders)
    Vector2 defenderPosition = new Vector2(defender.transform.position.x, defender.transform.position.y);
    if (positionToCheck == defenderPosition)
        return true;
return false;

and OnMouseDown I did this:

    Vector2 positionToInstantiate = SnapToGrid(CalculateWorldPointOfMouseClick());
    if (!IsPositionOccupied(positionToInstantiate))

I hope this helps!!!:laughing:


Privacy & Terms