• 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
17
Question by morbidcamel · Sep 29, 2014 at 07:49 AM · guiunity 4.6recttransform

How to convert from world space to canvas space?

I have a screen space canvas and I simply want to move a "target" over an object in world space. My world space coordinate is obtained with a raycast and I do a debug draw to make sure it is correct. The problem is none of the following code is giving me the right result.

 void Awake()
     {
         rt = (RectTransform)t$$anonymous$$s.transform;
     }
 
     void Update()
     {
         if (currentSelection == null)
             return;
 
         // Translate world position to UI coordinates
         Vector3 pos =  ((RectTransform)rt.parent).InverseTransformPoint(currentSelection.position);
         rt.position = pos; // nether rt or rt.parent works!
 
     }
Comment
Add comment · Show 1
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 morbidcamel · Oct 03, 2014 at 02:17 AM 0
Share

16 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Oct 03, 2014 at 03:10 AM

I hadn't the time to play around with the beta yet. So i can't say much about the new UI system, but the canvas and the RectTransform positions are pure screenspace / canvas coordinates and have no relation to "worldspace". Worldspace coordinates are actually mapped to screenspace / viewspace coordinates when rendered by a camera. If you want to know the screen / view space coordinate of a worldspace object, you have to use Camera.WorldToScreenPoint or Camera.WorldToViewportPoint of the camera that renders that object.

If you have the position in screenspace you might want to use the RectTransform $$anonymous$$erarchy to get it as a relative position wit$$anonymous$$n a certain area.

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
53

Answer by Sylos · Nov 24, 2014 at 02:32 PM

I solved t$$anonymous$$s problem by doing the following:

 //t$$anonymous$$s is your object that you want to have the UI element hovering over
 GameObject WorldObject;
 
 //t$$anonymous$$s is the ui element
 RectTransform UI_Element;
 
 //first you need the RectTransform component of your canvas
 RectTransform CanvasRect=Canvas.GetComponent<RectTransform>();
 
 //then you calculate the position of the UI element
 //0,0 for the canvas is at the center of the screen, whereas WorldToViewPortPoint treats the lower left corner as 0,0. Because of t$$anonymous$$s, you need to subtract the height / width of the canvas * 0.5 to get the correct position.
 
 Vector2 ViewportPosition=Cam.WorldToViewportPoint(WorldObject.transform.position);
 Vector2 WorldObject_ScreenPosition=new Vector2(
 ((ViewportPosition.x*CanvasRect.sizeDelta.x)-(CanvasRect.sizeDelta.x*0.5f)),
 ((ViewportPosition.y*CanvasRect.sizeDelta.y)-(CanvasRect.sizeDelta.y*0.5f)));
 
 //now you can set the position of the ui element
 UI_Element.anchoredPosition=WorldObject_ScreenPosition;

Comment
Add comment · Show 10 · 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 ian_sorbello · Mar 23, 2015 at 01:55 AM 1
Share
avatar image Vahradrim ian_sorbello · Apr 17, 2017 at 02:46 PM 2
Share
avatar image michaelday008 Vahradrim · Jun 19, 2019 at 12:45 AM 0
Share
Show more comments
avatar image RawSpartano · Apr 25, 2015 at 01:00 PM 0
Share
avatar image Almamu · May 05, 2015 at 08:14 PM 1
Share
avatar image Almamu · May 05, 2015 at 09:21 PM 0
Share
avatar image pbastia · Oct 14, 2016 at 04:47 PM 0
Share
Show more comments
avatar image
0

Answer by BMayne · Nov 26, 2014 at 02:44 AM

RectTransform.GetLocalCorners(Vetor3[] array) will fill out an array with 4 elements. T$$anonymous$$s gives you in world space how big the canvas is.

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
38

Answer by leni8ec · Dec 13, 2014 at 02:15 AM

T$$anonymous$$s issue also has another solution:

 Vector2 pos = gameObject.transform.position;  // get the game object position
 Vector2 viewportPoint = Camera.main.WorldToViewportPoint(pos);  //convert game object position to VievportPoint
 
 // set MIN and MAX Anchor values(positions) to the same position (ViewportPoint)
 rectTransform.anchorMin = viewportPoint;  
 rectTransform.anchorMax = viewportPoint; 

 



Comment
Add comment · Show 11 · 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 Poculis · Jan 07, 2015 at 09:46 AM 0
Share
avatar image Bankler · Mar 09, 2015 at 03:09 PM 1
Share
avatar image TutiBueno2 · Mar 11, 2015 at 09:18 PM 0
Share
avatar image leni8ec · Mar 12, 2015 at 09:41 AM 0
Share
avatar image Almamu · May 05, 2015 at 09:23 PM 2
Share
Show more comments
avatar image
16

Answer by Stardog · Jan 10, 2015 at 09:05 AM

T$$anonymous$$s is the cleanest way I can find of making a UI element follow a world object's 3D position.

 public RectTransform canvasRectT;
 public RectTransform healthBar;
 public Transform objectToFollow;

 void Update()
 {
     Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(Camera.main, objectToFollow.position);

     healthBar.anchoredPosition = screenPoint - canvasRectT.sizeDelta / 2f;
 }

Reading Sylos's answer above helped.

Comment
Add comment · Show 2 · 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 Cameron_SM · Feb 20, 2015 at 03:52 AM 0
Share
avatar image Suhrahj · Dec 24, 2016 at 04:15 PM 1
Share
  • 1
  • 2
  • 3
  • 4
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

61 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 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 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

Place UI element on pointer up position. 1 Answer

How to display notification on notification bar using GCM in Unity? 1 Answer

RectTransform position won't zero 0 Answers

Can I put non-GUI objects into Canvas? 1 Answer

Setting a Rect from another RectTransform 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