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
1
Question by grimmy · Apr 16, 2015 at 05:22 AM · uiscreenray

How can I get a ray working with new UI system?

The ray in the image below is supposed to fire exactly through the center of a moving little green square. The 2d square (FinalTargetLocator GameObject )is an image with a rectTransform in my canvas. The canvas use Screen Space - Camera.

I've tried lots of variants to get this working but nothing has been accurate. The closest I have come uses the following code but the ray is alway cast further from the centra of the screen than the green square. The further the square is from the centre of the screen, the more innacurate the ray. Infact the ray actually only seems to touch the green square at when the square is dead centre.

     finalTargetX=(finalTargetRectTransform.anchoredPosition.x +(Screen.width/2));//
     finalTargetY=(finalTargetRectTransform.anchoredPosition.y + (Screen.height/2));
 
     ray = UICamera.ScreenPointToRay (Vector3(finalTargetX,finalTargetY,0));
     
     Debug.DrawRay(ray.origin, ray.direction*1000, Color.blue);

alt text

2015-04-14-20-44-59.png (233.0 kB)
Comment
Add comment · Show 8
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 KdRWaylander · Apr 16, 2015 at 06:32 AM 0
Share

Why don't you fire the ray from the green square ?

avatar image Hrungdak · Apr 16, 2015 at 08:54 AM 0
Share

how is the green rect anchored? $$anonymous$$anual says:

If the anchors are not together, the four anchor positions are interpolated according to the pivot placement.

avatar image grimmy · Apr 16, 2015 at 12:14 PM 0
Share

kd -As far as I know I can only cast a ray from the camera to an object right? That's why I'm trying to cast from camera to the green square..

hrungdak - the pivot is right in the center of the object. see attached..alt text

2015-04-16-13-13-44.png (55.0 kB)
avatar image grimmy · Apr 16, 2015 at 12:17 PM 0
Share

..and anchors are all at 0.5

avatar image Hrungdak · Apr 16, 2015 at 07:50 PM 0
Share

Sorry, grimmy. I have rebuilt the scene and it works perfect.

The ray runs through the center of the target and if i move the target the ray follows exactly the center of it. I took the main Camera for the test, i don't know if your cam is the main. Is there is something wrong with your cam?

I had the effect that the ray wandered double or triple the way as the target. This was when the target was embedded in a panel that was smaller than the canvas. Solution was to divide finalTargetX and Y by Canvas.panelDistance (for what reason ever...). But without panel it works with your code.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

19 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

Related Questions

ShowAchievementsUI screen rotation 2 Answers

UI and resolution problems (fine in editor, problem in Android build) 1 Answer

Recttransform and Screen Resolution problem 0 Answers

UI elements following GameObjects (3d world) 1 Answer

Unity 5.6.1 Multi display - Second monitor shows as black 2 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