Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PlaidFlannelGeek · Mar 07, 2020 at 09:55 AM · spritetilemapdistanceraycasthit2d

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image streeetwalker · Mar 08, 2020 at 10:09 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PlaidFlannelGeek · Mar 08, 2020 at 01:02 PM 0
Share

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.

avatar image streeetwalker PlaidFlannelGeek · Mar 08, 2020 at 01:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges