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 /
  • Help Room /
avatar image
0
Question by ZaddBuzuki · Oct 25, 2016 at 04:01 PM · uigameobject2d gamedialogueanchor

How Do I Change UI Image Anchor Position To Hover Over GameObject?

Hello! I have a 2D game in Unity right now you control with WASD, topown.

I want to have a UI Object on screen during the game, it is just basic text and an image behind it for contrast and it will change throughout the game, but if your character were to walk in the direction the UI is on the screen, eventually when the gameobject comes into view, you'd see the UI lock on the game object, so you know that's what is speaking etc.

Like being able to hear something from far off, but once you approach it, you can see whats actually talking. you know?

Thanks for reading!

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

1 Reply

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

Answer by Crimx · Oct 26, 2016 at 06:42 AM

Hi, When you use canvas overlay, you can convert your game object position to screen position, like this

 public Camera mainCamera;
 Vector3 screenPosition = mainCamera.WorldToScreenPoint(yourGameObject.transform.position

then with screenPosition, you can use the value to set your UI position (don't forget to add some values so your UI position are not at middle of your game object).

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 ZaddBuzuki · Oct 28, 2016 at 09:36 PM 0
Share

Thank you for your answer! This is $$anonymous$$OST of what I am looking for... This does keep the UI Element ontop of whatever game object you like, however I still wanted it to get stuck on the edges of the screen you you can read it no matter where you are. Thanks to you, I was able to write a little bit of logic to restrain the UI element, but I still don't think it's entirely perfect. It is inside a C# Script inside the $$anonymous$$ain Camera, under the Update Function:

 Vector3 bubbleScreenPosition = gameObject.GetComponent<Camera>().WorldToScreenPoint(chatBubbleObject.transform.position);
         chatBubble.transform.position = bubbleScreenPosition;
         if (bubbleScreenPosition.y <= 0f + (chatBubble.GetComponent<RectTransform>().sizeDelta.y*gameObject.GetComponent<Camera>().aspect)/1.895f) {
             chatBubble.transform.position = new Vector3 (chatBubble.transform.position.x, 0f + (chatBubble.GetComponent<RectTransform>().sizeDelta.y*gameObject.GetComponent<Camera>().aspect)/1.895f, chatBubble.transform.position.z);
         } else if (bubbleScreenPosition.y >= gameObject.GetComponent<Camera>().pixelHeight - (chatBubble.GetComponent<RectTransform>().sizeDelta.y*gameObject.GetComponent<Camera>().aspect)/1.895f) {
             chatBubble.transform.position = new Vector3 (chatBubble.transform.position.x, gameObject.GetComponent<Camera> ().pixelHeight - (chatBubble.GetComponent<RectTransform>().sizeDelta.y*gameObject.GetComponent<Camera>().aspect)/1.895f, chatBubble.transform.position.z);
         }
         if (bubbleScreenPosition.x <= 0f + (chatBubble.GetComponent<RectTransform>().sizeDelta.x*gameObject.GetComponent<Camera>().aspect)/1.895f) {
             chatBubble.transform.position = new Vector3 (0f + (chatBubble.GetComponent<RectTransform>().sizeDelta.x*gameObject.GetComponent<Camera>().aspect)/1.89f, chatBubble.transform.position.y, chatBubble.transform.position.z);
         } else if (bubbleScreenPosition.x >= gameObject.GetComponent<Camera>().pixelWidth - (chatBubble.GetComponent<RectTransform>().sizeDelta.x*gameObject.GetComponent<Camera>().aspect)/1.895f) {
             chatBubble.transform.position = new Vector3 (gameObject.GetComponent<Camera> ().pixelWidth - (chatBubble.GetComponent<RectTransform>().sizeDelta.x*gameObject.GetComponent<Camera>().aspect)/1.895f, chatBubble.transform.position.y, chatBubble.transform.position.z);
         }


I made it work for multiple aspect ratios for those games were you can change window size on the fly, but I honestly have no idea what the number "1.895" has to do with anything, but this seemed to be a close number for what I was looking for to keep the UI element snapped/locked against whichever camera wall depending on your position. There still seems to be small gaps (padding) so its not totally touching the wall of the camera, so if anyone has any insight on how I can make this code a little cleaner, or give an explanation to why dividing by "2" is too much etc. that would be awesome!

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

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

How to Remove a Button after it has been pressed and executed it's script? 3 Answers

Why Does Prefab Loose UI elements on Scene Change 0 Answers

Image attached to the image component in a button is not visible during play mode 0 Answers

Automated level button system 0 Answers

Move gameObject on UI Button Press 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