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!
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).
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
![](https://koobas.hobune.stream/wayback/20220612095415im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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