Raycast distance in 2D sorting weird with multiple sprites
I've got a 2D scene with a hex-tilemap on it. I've set the Z for both the grid and tilemap to -1. There are then several sprites that I want to be able to drag n drop onto the tilemap and have them know where they are on the tilemap.
I have this all working fine with a single sprite. The moment I add/activate a second sprite in the scene, I can drop sprites onto the tilemap, but can't pick them up again. if I deactivate the tilemap, then the pickup works.
I've debugged this in a ton different ways and finally gotten to doing Raycasts from the MousePosition and looking at the order of the Raycast hits. In the situation where only one Sprite exists, the sprite always ends up at the top of the raycast array, which seems reasonable because the sprite is rendered on top of the tilemap.
When 2 sprites exist, the tilemap shows up at the top of the raycast array. In all cases, the "distance" of the Raycast2D is 0 for all entities regardless of what I do with the Z coordinate. Is there a way to affect this in 2D?
I am almost on the pathway to make a single ClickController that knows how to sort through the Raycast lists itself, but if I can use the OnMouse* events on the individual objects, I expect that would be easier to maintain.
I wanted to add that another approach is to use Layers, and when you do the RayCast you can choose which layers to filter out. So you can assign your tiles to one layer and then items to another. The RayCast can then ignore the tile layer completely. That way you don't have to do any if statements in your RaCast it check.
Answer by streeetwalker · Mar 08, 2020 at 05:25 AM
I have this all working fine with a single sprite. The moment I add/activate a second sprite in the scene, I can drop sprites onto the tilemap, but can't pick them up again. if I deactivate the tilemap, then the pickup works.
Where is the problem exactly? You want to pick up an individual item on your tile map where you have multiple items, and you don't want the tile to get in the way?
One approach is to tag your items and tiles with identifiers. So give all tiles a "tile" tag and items an "item" tag. Then in your ray cast hit get the collider.tag. If it is not "tile" then pick things up.
Hi, thanks for the response.
The problem is that the internal triggering of On$$anonymous$$ouse* events doesn't work as I would expect it. If a single Sprite and a single Tilemap are active, what is seen on the screen is how the On$$anonymous$$ouseDown is triggered. If there are multiple Sprites and the tilemap, then the render order is fine. However On$$anonymous$$ouseDown still registers the Tilemap ins$$anonymous$$d of the Sprite, when they share space on the screen, even though the Sprite is rendered on top.
To verify this, I did code to fire raycasts and check things. I was hoping that the On$$anonymous$$ouse events would work and not need to do the Raycasts, as I assume that is already done internally in Unity to make the On$$anonymous$$ouse methods work.
I think there is an easier way yet, other than doing the RayCast method with layers or tags. I just test this in 2d with multiple sprites over a tile sprite. Set the tile z positon to be a positive number (i.e. under the other sprites). Then the on$$anonymous$$ouseDown in a script on all objects fires as one would expect.
Your answer
Follow this Question
Related Questions
Tiles are only being removed if raycast fired between 0 and 90 degrees clockwise from character 0 Answers
Sorting layers without far objects overlapping closer ones? 0 Answers
Tilemap sprites disappearing when I go far away from it 0 Answers
TileMap in custom layer doesn't render 1 Answer
Checking for Raycast distances not working as expected. 0 Answers