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
2
Question by Aladine · Jan 22, 2014 at 10:54 PM · positionclickpixel

How to get pixel position when clicking an object ?

Hello,

The (0,0) position of an object texture is in the left-bottom of the object, suppose i clicked on the object in that exact position, how can i get these (0,0) ?

i tried to do something like this but it wasn't precise :

 objectPoint = Camera.main.WorldToScreenPoint (transform.position);
                 objectSize = new Vector3 (targetTexture.width, targetTexture.height, 0);
                 mousePoint = Input.mousePosition;
                 PixelPoint.x = mousePoint.x - (objectPoint.x - (objectSize.x / 2));    
                 PixelPoint.y = mousePoint.y - (objectPoint.y - (objectSize.y / 2));    

thank you

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
3
Best Answer

Answer by robertbu · Jan 23, 2014 at 01:21 AM

Generally this is done using Raycasting. You Raycast against the texture using one of the versions of Raycast that take a RaycastHit structure as a parameter. One of the things RaycastHit returns is the RaycastHit.textureCoord. This is the UV coordinate of the hit on the texture. UV coordinates go from 0 to 1, so you have to multiple the width and height by textureCoord.x aand textureCoord.y respectively to get the pixel position.

Comment
Add comment · Show 3 · 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 Aladine · Jan 23, 2014 at 01:24 AM 0
Share

$$anonymous$$y savior like usual ^_^ !! this sounds to be the perfect approach, even that am not very familiar with Raycasting, i will try it and tell if it worked or not, thank you very much

avatar image Aladine · Jan 23, 2014 at 01:38 AM 0
Share

and this give the perfect result,

 void Update ()
         {
                 if (!Input.Get$$anonymous$$ouseButton (0))
                         return;
         
                 RaycastHit hit;
                 if (!Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit))
                         return;
         
                 Renderer renderer = hit.collider.renderer;
                 $$anonymous$$eshCollider meshCollider = hit.collider as $$anonymous$$eshCollider;
                 if (renderer == null || renderer.shared$$anonymous$$aterial == null || renderer.shared$$anonymous$$aterial.mainTexture == null || meshCollider == null)
                         return;
         
                 Texture2D tex = (Texture2D)renderer.material.mainTexture;
                 Vector2 pixelUV = hit.textureCoord;
                 print ((int)(pixelUV.x * renderer.material.mainTexture.width) + "--" + (int)(pixelUV.y * renderer.material.mainTexture.height));
         }
avatar image celikmurat · Jul 16, 2015 at 08:23 AM 0
Share

Is there any solution we can handle this with Physics2D ?

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

Getting to move object with mouse position? 0 Answers

How do I get mouse position in game units? 3 Answers

freeze position on click 1 Answer

Spawn Objects Where i click 2 Answers

Make object go to point a to b on click 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