Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ucuri · Jan 23, 2014 at 11:47 PM · textureraycastspritecoordinatescalculation

Raycast on sprite in 3D space, how to find texture coordinates

Hi there, I ran into a problem that just makes my head melt. I got a sprite with a BoxCollider, they have the same width and height. They can be positioned anywhere in 3D space, but always face the player. My plan is to Raycast towards the sprite from the player, and paint a pixel at the point of impact. Now normally I could use RaycastHit.textureCoords for something like this, but I didn't got it to work with a sprite and a BoxCollider.

The approach I want to try is to just get the point of impact on the BoxCollider and then calculate the coordinates of the pixel that is below this point of impact.

A quick visualization of this, I hope this is helpful. First a raycast hits (the upper edge in this case, for visualization purposes) and then the pixel on the sprite is calculated.

alt text

But I can't seem to figure out how to do that. I tested a few (well, more than a few) things, and I think I need to watch out for the pivot point, width, height and the pixel to unit conversion of the sprite. The object can also be oriented just about anywhere in space, the only sure thing is that it looks at the player.

Here is just a quick sample of my code, I included two of my failed attempts. Both of them sometimes result in colored pixels, but not always.

 //C#
 SpriteRenderer renderer = hit.collider.gameObject.GetComponent<SpriteRenderer>();
 Texture2D tex = renderer.sprite.texture;
 
 //First attempt: in local space
 //Resulting pixels are just off by a pretty large amount
 Vector3 localPos = hit.transform.InverseTransformPoint (hit.point);
 Vector2 texturePos = new Vector2(localPos.x, localPos.y);
 texturePos.x = texturePos.x * tex.width;
 texturePos.y = texturePos.y * tex.height;
 
 //Second attempt: in world space
 //Pixels are off and rotation seems to be a problem
 Vector2 texturePos = new Vector2();
 texturePos.x = ((hit.point.x - hit.collider.bounds.min.x) / hit.collider.bounds.size.x)*tex.width;
 texturePos.y = ((hit.point.y - hit.collider.bounds.min.y) / hit.collider.bounds.size.y)*tex.height;

texturePos is supposed to hold the coordinates at the end. I would be really grateful if someone could help me with this, because this is a pretty crucial part of what I'm building.

(PS: If you can think of a better solution than this projection thing, then I would be happy too. I just need the right pixel coordinates.)

example.png (22.5 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

0 Replies

· Add your reply
  • Sort: 

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

18 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

Related Questions

Terrain Height Map Generated from raycast array[Help] 0 Answers

Wrong uv coordinates in Unity? (but works fine with Blender) 1 Answer

How can I access a sprites texture and add it to a GUI button? 0 Answers

Sprite Renderer is wrapping bottom pixel when it shouldn't be 1 Answer

Move object to specific coordinates 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