• 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 Romano · Apr 15, 2014 at 05:06 AM · uvraycasthit2dgetpixel

What is the easiest way to convert raycasthit2D to pixel coordinates?

I have a sprite with a box collider or polygon collider and I'm looking for a calculation that will translate any raycasthit2D on that sprite into the pixel coordinates of the texture so I can use the coordinates with GetPixel().

Does anyone have some code that will do this conversion?

There are quite a few posts on similar topics but nothing that has solved my issue. Someone had suggested translating the hit into barycentric coordinates, but I'm not great with maths and it blew my mind a little bit. My project's at a standstill while I have this issue so any help will be appreciated massively :)

Thanks!

Romano

Comment
Add comment · Show 2
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 robertbu · Apr 15, 2014 at 05:51 AM 0
Share

I'm sure this can be worked out (though the calculations might get a bit ugly), but I wonder if approaching the problem a bit differently might make things easier. $$anonymous$$ake a Quad a child of your sprite and size it to the same size as your sprite. Then you can do a Physics.Raycast() (i.e. 3D raycast). A RaycastHit structure gives you back the uv (RaycastHit.textureCoord) which is easy to turn into a pixel coordinate.

I also have to ask if you really need a sprite. People seem to automatically use them for 2D, but in many situations a Quad could be used ins$$anonymous$$d.

avatar image Romano · Apr 15, 2014 at 06:09 AM 0
Share

Hi there. That's an interesting suggestion, thanks. It's a 2D game full of sprites with box and polygon colliders attached. I'm already having to use 2D raycasts to detect clicks on the the collider2Ds so if possible I'd like to avoid having to do 3D raycasts too. It'd be so easy if textureCoord was included in the raycastHit2D.

I don't know much about using quads but my sprite will be animated and the quad might change size, which could start getting tricky.

1 Reply

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

Answer by fafase · Apr 15, 2014 at 05:32 AM

Considering you will hit on the edges of the sprite, you can take the position of the hit, find the local position of that hit, simply doing

       LocalPos= sprite.position-hit.position

Then you can convert that local position into texture position using the width and height of it.

      Uv = localPos.y/ height *textureHeight



This is considering you hit the side of it.

This is simplified but maybe you get something out of it.

EDIT: You turn the hit point to local position of the sprite. Then you do the process of conversion for both dimensions (x and y).

All you do is converting from one coordinate to another. For instance, if you have a sprite that has dimensions 2x2 at position (10,1)and you hit at position (9.5,0.5). Your texture is a 256x256.

First get the hit in sprite position:

 10 - 9.5 = 0.5
 1 - 0.5 = 0.5

Turn that into texture coordinates:

 value * scale * textureSize
 uv.x = 0.5 / 2 * 256 -> 64
 uv.y = 0.5 / 2 * 256 -> 64

64,64 is the pixel position of your hit.

Comment
Add comment · Show 8 · 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 Romano · Apr 15, 2014 at 05:44 AM 0
Share

That looks great, I'm not sure I understand it all though. Could you explain what you mean by hitting the edges or the side of the sprite? The raycasthit2D would be a mouse click which could be anywhere within the collider.

I'm also not sure what position-hit should be replaced with in the code?

Thanks!

avatar image fafase · Apr 15, 2014 at 06:16 AM 0
Share

Updated the answer

avatar image Romano · Apr 15, 2014 at 06:20 AM 0
Share

I think I get it, thanks. I'll try it out!

Just to make things more complicated, this wouldn't work if the sprite mode was "$$anonymous$$ultiple" would it? i.e if the sprite was animated and the texture was changing all the time.

avatar image fafase · Apr 15, 2014 at 06:57 AM 0
Share

I can't find in Animator or Animation (though there is probably one) any method returning what part of the sprite is currently used. But in the case there would not be, you can easily find which is by using the parameters you set for animation.

First you can get the current state: http://docs.unity3d.com/Documentation/ScriptReference/Animator.GetCurrentAnimatorStateInfo.html

Then you get the time https://docs.unity3d.com/Documentation/ScriptReference/AnimatorStateInfo-normalizedTime.html

it tells the fractional part is the normalized time the animation has been running.

Then you know how many sprite there is on your animation and you just do the math.

if 24 states in your animation and you are at 0.3 of the animation 0.3 * 24 = 7

you are on state 7 of your animation.

Now if your sprite sheet is 1024 width and all states are aligned:

 1024/ 24 * 7 = 298 

is the starting x coord. You just use the values you found previously and you add them to those and you get your uv on the whole sprite sheet.

avatar image Romano · Apr 15, 2014 at 07:14 AM 0
Share

Wow, thanks for the lengthy answer :) I thought it would be simpler to use

     spriteRenderer.sprite.bounds;
 OR
     spriteRenderer.sprite.rect;

but so far getting nothing but 0,0,0,0 no matter what part of the texture is displaying. Am I just doing something totally wrong and could fix that or is your method the only way?

I wondered if my method could work because my animations are purely sprite based, there's no messing with position or rotation.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Assigning UV Map to model at runtime 0 Answers

Animation with UV Warp Modifier from Blender 2 Answers

Send Custom Network Message Unity Multiplayer 0 Answers

CustomRenderTexture ignores "ComputeScreenPos" 1 Answer

Using GetPixel to get the alpha of a pixel 1 Answer


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