Hello everyone! I am making a 2D game, and want a UI Element to be like a Chat box you can “Hear” from a distance, Its size is 150 Width by 60 Height, and is just a regular texture element I can overlay text on, but I am having trouble automatically positioning it on the screen towards the position it wants to snap to. The code I currently have below “Should” do just that, but doesn’t… Its a C# script inside the Main Camera itself. “chatBubble” is the Sprite object in Canvas. and “chatBubbleObject” is just a gameObject in real space I want it to lock to when its visible on screen etc:
Vector3 bubbleScreenPosition = gameObject.GetComponent<Camera>().WorldToScreenPoint(chatBubbleObject.transform.position);
chatBubble.transform.position = bubbleScreenPosition;
if (bubbleScreenPosition.y <= 0f + chatBubble.GetComponent<RectTransform> ().sizeDelta.y) {
chatBubble.transform.position = new Vector3 (chatBubble.transform.position.x, 0f + chatBubble.GetComponent<RectTransform> ().sizeDelta.y, chatBubble.transform.position.z);
} else if (bubbleScreenPosition.y >= gameObject.GetComponent<Camera>().pixelHeight - chatBubble.GetComponent<RectTransform> ().sizeDelta.y) {
chatBubble.transform.position = new Vector3 (chatBubble.transform.position.x, gameObject.GetComponent<Camera> ().pixelHeight - chatBubble.GetComponent<RectTransform> ().sizeDelta.y, chatBubble.transform.position.z);
}
if (bubbleScreenPosition.x <= 0f + chatBubble.GetComponent<RectTransform> ().sizeDelta.x) {
chatBubble.transform.position = new Vector3 (0f + chatBubble.GetComponent<RectTransform> ().sizeDelta.x, chatBubble.transform.position.y, chatBubble.transform.position.z);
} else if (bubbleScreenPosition.x >= gameObject.GetComponent<Camera>().pixelWidth - chatBubble.GetComponent<RectTransform> ().sizeDelta.x) {
chatBubble.transform.position = new Vector3 (gameObject.GetComponent<Camera> ().pixelWidth - chatBubble.GetComponent<RectTransform> ().sizeDelta.x, chatBubble.transform.position.y, chatBubble.transform.position.z);
}
(Visual Image of whats happening, I Don’t want those gaps…):
The main issue I am having, Is I could have sworn I would have needed to Divide the Length of the UI object by Half, but to get the best results, I did not do this. Plus, It is still not completely accurate in snapping to the screen, and when the screens aspect ratio changes, it does throw off the UI’s snap, making a big gap show, So basically it’s not correct math.
I have a theory that the UI Size of the object 150 Width by 60 Height does not correctly translate to positions relative to the camera. I managed to see screen position relative to a game object, but I am confused if I need to do the same with the canvas to the camera, or how I would even go about doing that.
Thanks for reading!