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 /
avatar image
0
Question by MarcoComolli · Dec 13, 2017 at 05:17 PM · 2d gamespritestouch controlslayer

Unity 2D: query for "mask/layer" bit on screen space without collider

Hi all, I'm currently working on a 2D game.
I have a bunch of sprites (currently with only black or transparent areas) displayed on the screen and I would like to know if an user input (actually is an input touch but could also be a mouse click) is in the black area of my sprite.

The touch input comes in screen coordinates so I had in mind to do something like this:

  • Receive the user input at the screen point X

  • Look at what is displayed by the main camera at the point X, in a certain layer (I need to query only to these kind of sprites)

  • If the alpha (or the color) of that corresponding point is 0 -> do semething, else -> do other things

I can get the world coordinates of the point with the ScreenToWorldPoint() method but I don't know how to get some data associated to the sprite in that position. I need some sort of binary mask of the screen (like a 0-1 accessible camera depth buffer or idk).

I would like to avoid using colliders because the sprites could be somewhat irregular as this: alt text

and i need to be accurate with the checks. The code will be placed in the Update() method so it can't be too much burdensome to be processed.

Any idea or place to be pointed to?

Thanks in advance

ttt.png (38.6 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MarcoComolli · Dec 16, 2017 at 07:23 PM

It took me a bit but I've found a solution, maybe not the optimal one or the most elegant but for me it works.


For any other interested I've used one RenderTexture with these steps:

  • Create a RenderTexture (for my checks I use a 32x32 texture, don't use a big texture resolution in the update cycle or the performances reduce drastically)

  • Create a secondary camera with culling mask to the selected layer (the one with the mask sprites)

  • Select the texture as the camera target texture (adjust camera setting to render only a portion of 32x32 pixel of screen >>> check the orthographic size for this).

  • Make a script to place the camera where the touch input arrive

  • Create a texture2D from the RenderTexture to access the mid pixel of the new texture generated


The cons is that actually (if I'm not mistaken) the accessed pixel is 1 frame later compared to the actual update cycle because of the camera OnPostRender() method, but 1 frame is quite irrelevant in my case.

Hope it helps.

Comment
Add comment · 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

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

80 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

Related Questions

Sprite managed by Sprite Renderer doesn't shows up in play mode 0 Answers

2D game camera background between sprites 0 Answers

How do I get the sprite pointing the right direction? 1 Answer

Spritest look squashed and stretched in different screens. 1 Answer

What do you put in the Project Settings>Graphics>Transparency Sort Axis, and what does those values mean (e.g. the difference between x = 1 and x = 69) ? 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