Select Object by RayCast2D
-
Hi!
I need to make an object selection system. As I understand it, it can be implemented via RayCast2D - I change RayCast2D.Position to the mouse position and objects are detected. But the problem is this: In my game, some objects are one under the other and GetCollider() returns two objects. I need only the topmost object to be selected, and the one below it is not selected. How to implement it? -
Assuming the z index is not different, you can use node.get_index()
https://docs.godotengine.org/en/4.1/classes/class_node.html#class-node-method-get-indexSince z_layering is sorted by position in scene tree. So just compare them to get the topmost node.
-
@Salodo Thanks a lot!
I forgot to mention that I need to change the sprite color on hover. After your answer, I came up with the idea of how to google the question and I found a solution on reddit. I wrote a function where I use the GetIndex() method: "I managed to do it, by keeping track of all the sprites that can be highlighted and then doing a first pass to checking the highest z_index or child_index."
-
Haha, yeah googling it is a good idea