
Innovate / Suits Tower Defence / Log #4
Grid Interaction
So I had a huge to-do list planned for this one. I said "Okay Marty, this is what's up:"
-
Create a HUD button to simulate "Entering tower placement mode"
-
Build in mechanics to track the mouse to engage with our tiles
-
Snap-lock transparent towers into the grid and click to place
-
Have the grid react once you've placed a tower
-
Show tower's area of attack before placing
Okay, look. So on that list I actually only did the first two. I've had a shitty night and a pretty heated argument with a friend so stomaching through that and actually making progress has been an absolute colossal challenge. Okay so first things first, a button.

Kind of a nice reminder to RTFM (Read the fucking manual). In contrast to some recent cases, stack overflow was actually sufficiently trumped by Unity's docs, so I set up camp there. Getting the button hooked up with some event listeners on instantiation was refreshingly logical and straightforward. Ran into some road blocks because I couldn't click the button? Did some looking, tried moving the button in front or behind the HUD canvas to see if it was in the way or something. Took una momento but I found this fucking brilliant post response:

Turns out I needed to add an "EventSystem" on my prefab and once I added it, worked a charm. Wired it up to kick off our aforementioned grid glowing "animation" and actually toggle its rendering; but I ran into some annoying issues around the glowing effect whereby toggling it off when it was at the darker end of the glow would permanently lock it in that alpha. Took a little tweaking but it was fixed when I added some flooring on some comparisons (because pausing it didn't stop a slight lag of timers ticking over what they should have, don't ask) and a few operator changes.


But yes there she is, dumb button make grid go boop. Next up I knew with my not-feeling-so-well, I realised I wouldn't have much more development time before I also wrote this article, so I shortened my ambitions of my to-do list to just proving not only that my mouse could hover over a cell, but it could interact with it too. So I made a small proof of concept that did just this, but only when the grid was turned on (which would be functionality I needed anyway).

Playing with the docs, but using a "RaycastAll" instead to pierce through all the random colliders I had up, I managed to start selecting grid components. I was a bit shook because I felt this would be a performance hit from the way I wrote it; constantly looping through everything in our raycast every single frame. It actually performs so smoothly in parallel with the shit performance of my speeding enemies (that's future Marty's issue :) ).
So yeah as you can see in the below screenshot, I set it up so that while the grids were visible, if my mouse hovers over an element it'll just disable the tile. I swung the mouse around and as we can see there's some missing grid squares. I have effectively managed to create a prototype of tower placement.

