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 Lazybones94 · Sep 25, 2015 at 12:26 PM · spritespixels

Pixel of a Sprite under the MousePosition

It sounds almost easy, but I can't find any solution for two days already: I have a 2d object(its a sheet of paper) with some coordinates, and when I click on it I want to get the pixel where I clicked. And then draw around this pixel. This specific pixel where I clicked must be not in world coordinates(not a screen pixel), but a pixel of this sheet of paper. How do I do it?

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 Lazybones94 · Sep 25, 2015 at 05:57 PM

Solved it with big effort. To get the needed pixel, I had to cast two rays: one ray from the bottom middle, another from left middle parts of the screen. This gave me two Vector2 with the least X and Y positions of my sheet of paper in world coordinates. Then I get the mouse position in world coordinates, and calculate the length of X line from the least X position to mousePosition.x and the length of the same line in Y axis. Then I calculate the relation of these two lines to the full X and Y length, and use this relations to calculate the needed pixel. Here is the code example:

Vector2 screenMidX = new Vector2(Screen.width * 0.5f, 0); Vector2 screenMidY = new Vector2(0, Screen.height * 0.5f); RaycastHit2D YPointHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(screenMidX), Vector2.up); YLowerPoint = YPointHit.point; RaycastHit2D XPointHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(screenMidY), Vector2.right); XLowerPoint = XPointHit.point; float XdeltaPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - XLowerPoint.x; float YdeltaPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - YLowerPoint.y; float Xpart = 1 /( (paper_currentSprite.bounds.size.x * paper_current.transform.lossyScale.x) / XdeltaPoint); float Ypart = 1 /( (paper_currentSprite.bounds.size.y * paper_current.transform.lossyScale.y) / YdeltaPoint); int pixelsX = paper_currentSprite.texture.width; int pixelsY = paper_currentSprite.texture.height; int posX = (int)(pixelsX * Xpart); int posY = (int)(pixelsY * Ypart); And the posX and posY integers represent the position of the needed pixel. Hope somebody will find this post useful one day!

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

29 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

Related Questions

Sprites have discolored patches / color bleeding 0 Answers

Can you use transform.Translate with pixels instead of units? 0 Answers

Loading array of objects by using parent folder name in asset bundle? 0 Answers

Is there a quick way to convert a sprite sheet of sprites to a bunch of prefabs? 0 Answers

Moving MANY objects together 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