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 DCTShinobi · Mar 05, 2015 at 06:44 AM · uiraycastcoordinatesworldspace

UI issue: Getting screen coordinates from a World Space object

I've been writing scripts for UI objects using a canvas set to Screen space - Overlay. It's pretty simple to get the side/corner coordinates from all of the (rect)transforms. However, I'm attempting to modify the scripts to also work with canvases set to World Space.

The graphic raycast I'm using seems to work fine. However, I am not sure how to get the screen coordinates for the (rect)transforms.

I've read some about "Camera.WorldToScreenPoint," however, I don't know how that would tell me where the UI object's edges are located without me already knowing.

Is there a way for the graphic raycast to tell me how far inside of the object's borders it is?

Using Screen space - Overlay, I just need to get the position and size of the object, then it's simple math. But for an object that could be any distance from the camera... or not even on the camera at all... well, that's where I'm lost.

Thank you!

Comment
Add comment · Show 3
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 hexagonius · Mar 05, 2015 at 06:51 AM 1
Share

Your question is confusing. Do you want the distance to the side of an object, distance to the camera or screen coordinates?

avatar image DCTShinobi · Mar 05, 2015 at 10:50 AM 0
Share

I apologize. What I'm ultimately looking for is how to get the screen coordinates of a given edge of a UI object such as a panel if the canvas in set to use World Space.

I usually use a canvas set to Screen Space - Overlay. Because it automatically fills the screen, it is relatively simple to get the screen coordinates for an object's edges. I merely need to get the width/height of the object in question, divide it in half, then add/subtract that value to the object's position coordinates. When I compare that number with the canvas' coordinates, I then have the exact location of a given edge.

But for a canvas that does not fill the screen perfectly, and can dynamically change scale and position (compared to the screen), I don't know where to even begin to get a reference point to compare the UI object to.

I suppose one main problem is that, according to the object's RectTransform, the height and width never actually change no matter how far away from the camera it gets. So I can't use the RectTransform info to base coordinates on as I do with the canvas set to Screen Space - Overlay.

Ah, and I think I may have just made everything more confusing. Again, I apologize. I think 6am isn't a good time for me to try and explain things, haha! Thank you, though.

Ps. I'm wanting to use the coordinates so I can define exactly where my cursor can interact with the UI object. $$anonymous$$y raycast allows me to interact with the object if my cursor is ANYWHERE over the object. But if I wanted interaction to take place ONLY if the cursor were, for examples, 25 pixels inside the object's edges, I don't know how to get an edge reference.

avatar image DCTShinobi · Mar 06, 2015 at 06:13 AM 0
Share

Thank you so very much! I'll put that into practice!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Mar 05, 2015 at 08:16 PM

I don't know about the UI, but what I know is: if the canvas has a world space position and you get the width into units you can calculate a bordervector. transform.position (of the UI) minus transform.right * (width / 2), would give you a point on the left edge of the canvas. In world space. You could THEN Camera.main.WorldToScreenSpace(leftEdgePoint) to get the pixel coordinates of that point on screen. Then it's just a small step to the pixel difference of the cursor.

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

21 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

Related Questions

Raycast from 2D canvas object to world space 1 Answer

UI element in 3D space, reacts to click despite raycast target being infront 0 Answers

Can't interact with buttons in a UI Canvas in World Space Render Mode. 0 Answers

Different between IPointerClickHandler.OnPointerClick and MonoBehaviour.OnMouseDown 1 Answer

Most direct way to create OnCursorStay "event" in UI. 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