Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 juniperspark · May 18, 2016 at 05:08 PM · buttonpositionrecttransformworldtoscreenpoint

WorldToScreenPoint is giving me a wrong X position?

I want to have a Unity GUI button float over an instantiated Game Object.

I have parented a Canvas and it's button to the prefab and have attached the follow script to the button (and filled the correct variables), but it positions the button entirely in the wrong position. What have I done wrong? Is this code correct? I have tried using both WorldToViewportPoint and WorldToScreenPoint, but I am given a wrong X position.

My canvas covers the whole screen and my button sits in the middle by default.

 var characterObject     : GameObject;
 var button         : GameObject;
 var camera        : Camera;
 
 function Start()
 {
     ObjectToScreenPosition();
 }
 
 function ObjectToScreenPosition()
 {    
     var buttonPosition : Vector3 = camera.GetComponent(Camera).WorldToScreenPoint(CharacterObject.transform.position);
     button.GetComponent(RectTransform).anchoredPosition = buttonPosition;
 }
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

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by juniperspark · May 18, 2016 at 06:50 PM

The solution to this was that my canvas of the button I wanted to move was not set to Screen Space - Overlay.

Comment
Add comment · Show 1 · 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 VeTaL · Oct 15, 2018 at 11:01 AM 0
Share

With Screen Space - Camera mode, i believe, the issue is the camera size. I'm not sure how to pick it properly, so i'll just stick to Screen Space - Overlay, it works for me as well.

avatar image
0

Answer by tanoshimi · May 18, 2016 at 05:34 PM

On line 13, try the following instead:

 button.GetComponent(Transform).position = buttonPosition;
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 juniperspark · May 18, 2016 at 05:48 PM 0
Share

It still doesn't work. The position I should be getting is something like: -481, 210, 0. but the position it is now spitting out is: 0.2, 0.4, 6.2.

I have tried changing anchor points on the button, but that doesn't seem to change anything. The Canvas is set at 0,0,0 with a 0.69 scale factor. The button is set at 0,0,0, with a 1 scale factor.

$$anonymous$$y camera is a Perspective camera with 60 FOV and a rotation of 40, 330, 350.

Would any of this be a problem?

avatar image tanoshimi juniperspark · May 18, 2016 at 06:24 PM 0
Share

Ah hang on, I read your post more carefully and I can spot some problems. "I have parented a Canvas and it's button to the prefab". Don't do that - you're setting the position via this script, so you don't want the canvas to inherit the position of any parent object. Place your canvas directly at the root of your scene hierarchy and try again. Also, be sure that your canvas is set to screenspace. There's also something weird going on with your z values. Here's the exact code that works for me when attached to a screenspace canvas to anchor it to any position in world space:

 // Update is called once per frame
 void Update () {
     Vector2 sp = Camera.main.WorldToScreenPoint(anchor.position);
     thisTransform.position = sp;
 }
avatar image juniperspark tanoshimi · May 18, 2016 at 06:54 PM 0
Share

Hi again,

Sorry I read your post a little late. You are correct, the screenspace on the canvas was wrong and that was causing the errors. I had it set to screenspace camera ins$$anonymous$$d of overlay. Setting it to overlay seemed to solve the problem.

avatar image
0

Answer by Jessespike · May 18, 2016 at 06:11 PM

hmm, I'm not really happy with this solution, but it works I guess. Surely there's a more elegant way to approach this. Well, at best it might give you some ideas how to resolve the issue. The snippet you posted contained script errors for me, so I renamed things a bit.

 var m_characterObject : Transform;
 var m_button : GameObject;
 var m_camera : Camera;
 
 function Update()
 {
   ObjectToScreenPosition();
 }
 
 function ObjectToScreenPosition()
 {    
   var buttonPosition : Vector3 = m_camera.WorldToScreenPoint(m_characterObject.position);
   m_button.GetComponent(Transform).localPosition = Vector3(
     buttonPosition.x - (m_button.transform.parent.GetComponent(Canvas).pixelRect.width / 2),
     buttonPosition.y - (m_button.transform.parent.GetComponent(Canvas).pixelRect.height / 2),
     0);
 }
Comment
Add comment · Show 1 · 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 juniperspark · May 18, 2016 at 06:47 PM 0
Share

Thanks for your answers and your code. @tanoshimi @Jessespike

I came across a couple of problems, one of which was randomly fixed and I can't tell what caused it: for some reason another script was interfering with it, which had nothing to do with that button. but I reverted to an old scene before I was mucking about and it sorted that one.

The main problem I think was that my canvas was set to Screen Space - Camera and changing this to Screen Space - Overlay fixed this.

avatar image
0

Answer by Eyevi · Aug 29, 2018 at 01:48 PM

My problem was the same, but I found a very easy solution (even with "scale with screen size")

Code:

         Vector3 viewPos = Camera.main.WorldToViewportPoint(anchorPoint.position);
 
         GetComponent<RectTransform>().anchorMin = new Vector2(viewPos.x , viewPos.y );
         GetComponent<RectTransform>().anchorMax = new Vector2(viewPos.x , viewPos.y );

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

47 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

Related Questions

Make position of Buttons Elastic. 0 Answers

GUI Button Transform Screenspace 0 Answers

UI element moves down when another UI elements gets too close? 0 Answers

RectTransform.position of Panel always 0 in FullHD 0 Answers

Is there a way to lock GUI buttons to a screen position insted of a pixel defined position so it will show up no matter the resolution of the screen? 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