Detecting collisions from the mouse?

Godot Version

4.3

Question

Hi I am trying to detect collisions with my game tiles. I was using a tile map before but it became a bit too limiting as I want to have lots of tiles that can be interacted with so I decided to make each tile its own node and recode a lot of stuff. The hardest thing about it though is actually getting which tile the mouse is hovering over.

I was using an area 2d with the mouse entered and exit signals to get the tile being hovered over. If the mouse entered a tile the tile would call a function in my world script, passing itself as a parameter, which I could then use to remove tiles / place tiles. However when tiles were adjacent to each other the mouse exit signal would fire after the mouse enter signal. So the next tile hovered over would update the tile hovered over, and then the exit signal from the previous tile would fire which set the hovered tile to equal null which basically meant the game thought I was hovering over no tile when I was actually.

Is there a simpler easier way to handle this? Ive been really stuck for 2 days now. Ive seen some stuff on raycasting but this is such a simple thing and It doesnt seem like it should be this complicated. I feel like im just missing something. Does anyone know what I could do? Thanks

I haven’t checked for a better way to use the mouse on tiles (nor used tiles), so there may well be a better solution. The enter/exit signals seem reasonable though. Have you tried (in the exit signal) only setting the hovered tile to null if it is currently pointing to the tile calling exit?

If I understand correctly, this should prevent your exit signal from overriding the actions of your enter signal while still allowing you to clear a tile when exiting into a tile-less space.

It turns out raycasting actually worked brilliantly. Ive been working a lot today and my game is fully functional with this new system of each tile being its own node / scene.

If anyones curious and needs help, I made a get_tile function that shoots a ray at the mouse position and returs block_raycast.get_collider if block_raycast.is_colliding is true, and returns null if its not colliding. I then just use this function to check if theres a collider at the position of the mouse. It works very very well actually and is as good as the tile maps (well apart from performance. I cant have as many tiles on screen before it starts lagging as I could have with tile maps)

2 Likes