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 tonyjoseph456 · May 26, 2015 at 06:28 AM · ui imagerecttransformworldspaceworldpos

Rect Transform to World Position Conversion

Hi,

I'm new to unity's new UI system, Can someone please help me in my problem. I have a UI image in my 2d game containing two cameras. The UI image is shown using a camera for showing UI objects only. and the main camera is used to show game objects in World Space. The UI image should be moved to the position of another game object in World Space. I have tried several methods to move it, but it is not working cause, the conversion from rect transform of UI Image to the World space of WOrld object is not working correctly. Can someone please help me regarding the conversion from Rect transform component of UI image to World position.

Thanks in advance. Sorry to disturb you, but I had tired because of this conversion error.

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 moh05 · Apr 07, 2016 at 01:36 PM 0
Share

Hello, I was wondering the same. Did you got a solution? thanks

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Griffo · Apr 07, 2016 at 01:40 PM

 //    first you need the RectTransform component of your canvas
         var CanvasRect : RectTransform = 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 this,
     you need to subtract the height / width of the canvas * 0.5 to get the correct position.
 */    
         var ViewportPosition : Vector2 = cam.WorldToViewportPoint(target.position);
 // clamp the grenade to the screen boundaries
         ViewportPosition.x = Mathf.Clamp01(ViewportPosition.x);
         ViewportPosition.y = Mathf.Clamp01(ViewportPosition.y);
 
         var WorldObject_ScreenPosition : Vector2 = new Vector2(((ViewportPosition.x*CanvasRect.sizeDelta.x)-(CanvasRect.sizeDelta.x*0.5)),
                                                                 ((ViewportPosition.y*CanvasRect.sizeDelta.y)-(CanvasRect.sizeDelta.y*0.5)));
 //    now you can set the position of the ui element
         rectTransform.anchoredPosition = WorldObject_ScreenPosition;
Comment
Add comment · Show 3 · 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 moh05 · Apr 07, 2016 at 01:46 PM 0
Share

Oh great, will give this a try...Thank you :)

avatar image moh05 · Apr 07, 2016 at 02:07 PM 0
Share

Does it work if my canvas is set to World Space?

avatar image Griffo · Apr 07, 2016 at 02:16 PM 0
Share

I use Screen Space - Overlay

avatar image
-2

Answer by aditya · Apr 07, 2016 at 02:51 PM

why don't you just set the canvas space to world space

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Converting Rect transform to World Position of 2D game 1 Answer

How to get UI RectTransform bottom Y position? 1 Answer

How to find the bottom position of a Image based on it's height? 2 Answers

Assign RectTransform to Array based on another Array 0 Answers

Render Player infront of UI while maintaining world space 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