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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Morgan · Jan 28, 2015 at 07:04 AM · uiraycast

Unity 4.6 UI: get camera ray through a button?

I have a responsive UI (Screen Space - Overlay) and I want to add a 3D object spinning behind a certain button. (It's just a menu screen, luckily, so there are no concerns about collisions or lighting.)

To position that object, I need worldspace coordinates. So I want to cast a ray from the camera though the button.

What can I do, equivalent to "ScreenPointToRay" that will give me a ray from the button? (Center, corner, whatever... I can work with it!)

Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Jan 28, 2015 at 10:00 AM

...

You can use Transform.position and Camera.ScreenPointToRay.

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
avatar image
0

Answer by Morgan · Jan 28, 2015 at 06:47 PM

Thanks! It looks like there may be a bug, though, when using that with a CanvasScaler's Constant Pixel Size scaleFactor set to something other than 1 (as you need to do for retina displays):

EDIT: I think I have pinpointed the bug: http://forum.unity3d.com/threads/reference-new-4-6-ui-object-position-in-worldspace.267082/ (My workaround was to recreate manually the math that positions the UI button, so that I don't have to access the new UI system at all to get the coordinates. Worked in my case, although it's extra complexity.)

The following DOES work with scaleFactor = 1, but otherwise, the reported coordinates shift in a way I can't explain.

Here's what I've tried, in case it helps anyone. This places a 3D object behind a Unity 4.6 UI button (Screen Space - Overlay), pushes the object away from the near-plane so it's not clipped in half, and scales the object so it remains a constant pixel size regardless of device display size (which my 2D button does naturally via Canvas Scaler, but a 3D object does not: it would appear larger on larger screens).

UnityScript:

 var button2D : GameObject;
 var object3D : GameObject;
 
 var setBack : float = 2.5; //Distance beyond camera near-plane to render object (otherwise object will clip)
 
 var referenceScreenHeight : float = 1024; //Object already scaled to look right at this arbitrary screen size; must re-scale to compensate for other screen sizes (assuming UI Canvas uses Constant Pizel Size)
 
 ...
 
 object3D.transform.position = Camera.main.ScreenToWorldPoint( Vector3( button2D.transform.position.x, button2D.transform.position.y, Camera.main.nearClipPlane + setBack ) );
 
 object3D.transform.localScale *= referenceScreenHeight / Screen.height;


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

20 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

Related Questions

UI System not blocking Raycasts on Mobile only 2 Answers

[Solved]Cursor emulation with canvas? 0 Answers

(4.6 UI) Getting the world position of a raycast result 1 Answer

Correct use of transform.position of UI element into game screen 1 Answer

Raycast hit ignores everything except for the terrain 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