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 Filya · Sep 28, 2010 at 12:23 AM · mouseclickcoordinatespixel

Get Pixel Coordinate When Click On 2D Object

Hei all,,

Sorry if this has been asked before, but I couldn't find a good solution to it yet.

I have one object for example cube. When I clicked on that cube, I wanna get pixel coordinate of the point where I clicked there .

Could you help me guys? Please... I dont have idea to solve.

Thanks

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 · Sep 28, 2010 at 07:06 AM 0
Share

Off-topic, but your CSIRO project looks very interesting. :D

avatar image Filya · Sep 30, 2010 at 02:04 AM 0
Share

;D thank you.. Thanksss alot for ur help ;D

1 Reply

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

Answer by · Sep 28, 2010 at 12:30 AM

Raycast from the camera to mouse position and use the RaycastHit.point to get the impact position. Then use Camera.WorldToScreenPoint to get the screenspace (pixel coordinate) of the impact.

function Update ()
{
   if ( Input.GetMouseButtonDown(0) )
   {
      var hit : RaycastHit;
      var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      if (Physics.Raycast (ray, hit, 100.0))
      {
         var hitPos : Vector3 = Camera.main.WorldToScreenPoint(hit.point);
         Debug.Log(hitPos);
      }
   }
}

You'll usually have better success finding answers if you break it down to its basic parts. The raycast (object selection) script came from another answer, and there are plenty of other questions about converting world space to screen space.

Comment
Add comment · Show 9 · 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 Filya · Sep 28, 2010 at 06:05 AM 0
Share

Thank you marowi, I'm already try it, but I got error message like this :

$$anonymous$$issingComponentException: There is no 'Camera' attached to the "display" game object, but a script is trying to access it. You probably need to add a Camera to the game object "display". Or your script needs to check if the component is attached before using it.

I've try to put camera in that cuba but still doesnt work. Do you have any idea?

;D

avatar image · Sep 28, 2010 at 06:17 AM 0
Share

Apologies! The WorldToScreenPoint function was assu$$anonymous$$g that the script was on the Camera object. I've added '.main' so it will find the camera tagged as $$anonymous$$ainCamera.

avatar image Filya · Sep 28, 2010 at 06:33 AM 0
Share

AHAAAAA... THank you! Its works!!! Thanks alot $$anonymous$$arowi ;D

avatar image · Sep 28, 2010 at 06:37 AM 0
Share

No worries! Remember to vote up answers that help you, and mark it as 'correct' if it solves your problem (by clicking on the tick next to the arrows). Cheers.

avatar image Filya · Sep 30, 2010 at 02:04 AM 0
Share

oke,, Im already vote that ;D Thank you marowi

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

OnMouseDown doesn't work... 3 Answers

Why aren't sprite pixels perfectly aligned on android? 1 Answer

Get transparency of one pixel 2 Answers

Converting pixel dimensions to Unity's floating-point coordinates 1 Answer

GLSL - Paint fragments in a certain view position 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