[SOLVED] How do you ignore mouse click on projectiles and attackers?

Although Ben exclaimed how great it is that the mouse click is actually blocked by the defender already placed on the grid, it’s not so great when you have many flying projectiles in the way. Another problem is that the blocking doesn’t always work if my defender has a smaller collision box smaller than the grid. At that point I could actually place multiple defenders in the same grid. That’s easy enough to fix by checking for an instance in the code before placing.

However, it’s driving me nuts on how to not get the mouse click to be blocked by the projectiles or any other characters on the grid for that matter. If you have played Plant vs. Zombie which is what this game imitates, you can place a defender right in front of the attacker just as it’s moving into the grid location you want. This effectively stops the attacker when you put something down the last minute which is great for game play. Also when the game gets very chaotic with all the projectiles and things all over the grid, you can still easily put defenders down.

It seems like a simple thing to do in concept, but so far all the forum I read discussed something about using Ray Casting block and can’t use OnMouseClick anymore. Is that the only way to ignore unwanted objects when clicking? @ben, do you have any suggestions?


OK, looks like nobody reads this message board anymore including Ben. Anyway, I figured out how to solve this issue.

I ended up using MaskLayer in conjuction with Physics.Raycast and custom messaging to send Mouse events to get the effect I want. I had to replace the GameCore which is a UI element with a Quad 3D Object in order to get the RayCast to work properly. Not sure whether Raycast is a 3D function and therefore I need a 3D object? LayerMask didn’t seem to do anything anyway since the 2D Collider doesn’t seem to interact with the Raycast. In anycase, I can now hover my mouse over any of the Defenders, Attackers, or Projectiles without getting a block in the OnMouse event. I wrote a custom hover shadow to show where the unit will be placed on the board and also put in the checking for an existing Defender on the game board to prevent placing a duplicate.

Anyway, since nobody reads this, I won’t go into details on how to do it unless somebody is interested.


Hi there, actually we do read but sorry about the slow response. You’ll appreciate it’s hard with over 200,000 students.

I’m really glad you got it solved, and what’s more grateful that you’ve shared the value with others.

Enjoy the course(s)

Thank you, Ben!!! You do read the forum. I understand that you can’t be everywhere at once with so many students. It’s ok though because sometimes having to figure out the answer myself makes me learn a lot more. So far I really enjoy your courses. Keep them coming :slight_smile:

Hi, yes I read the forums but rather fast as I’m a little stretched.

Enjoy the course and rest assured we’ll circle-back and fix any bugs as we find them

1 Like

Hey, I see you already solved this problem your own way, but I was having a similar issue and figured I could add my solution to the pile. I wasn’t familiar with physics raycasting, so I used Z transforms and sprite render order to achieve a similar thing.

I actually set the LevelCanvas to be in front of the attackers and projectiles using a Z transform of -5 on the LevelCanvas. This initially had the effect of hiding my sprites, but then I just set the SpriteRenderer’s “Order In Layer” to 1 for all of the sprites, and they came back.

Then I ended up setting placed defenders in front of the canvas using a -6 Z transform. My defender selector buttons were also behind, but I set them to -6 Z as well.

Vector3 SnapDefenderToGrid(Vector2 worldPoint)
	float x = Mathf.RoundToInt(worldPoint.x);
	float y = Mathf.RoundToInt(worldPoint.y);
	float z = gameObject.transform.position.z - 1f;
	return new Vector3(x, y, z);

Projectiles at this point were being spawned at the defender’s “Gun” position, in front of the canvas, but I just set a Z transform of the guns to 3, which put them back behind the canvas.

Now only placed defenders will block my mouse events.

1 Like

Privacy & Terms