- Home /
Proper way to center a game object into an UI image?
Hi guys, I'm currently trying to center a game object into an UI image, the thing is, I can't figure out how to do it, I'll try to explain with screen captures.
I have an image that acts as the container of the game object, not in the hierarchy though, and that container is not in the center of the screen but slightly more at the right. What I want is to place that game object in the center of the image.
If I put the game object in the position of the rect transform of the image then it gets centered as you can see below.
var container = GameObject.FindGameObjectWithTag("AvatarContainer").GetComponent<RectTransform>();
gameobject.transform.position = container.position;
But it does not matches from the camera view, it is even outside of the camera space. How can I get the correct position?
Thanks guys!
Answer by Artaani · Dec 13, 2016 at 08:02 AM
Example:
// Let's find a position (it will be position in pixels on the screen)
Vector3 containerPosition = GameObject.FindGameObjectWithTag ("AvatarContainer").GetComponent <RectTransform> ().position;
// Project screen position to world position using specific camera (main camera in this case)
float DesireDistanceFromCamera = 5;
gameobject.transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (containerPosition.x, containerPosition.y, DesireDistanceFromCamera));
Upd: Link on project folder (Unity 5.5)
Thanks for your answer, but it does not work, I already tried with ScreenToWorldPoint before asking here, the thing is that containerPosition is not in screen units so the resulting vector is wrong. I think the way to get the right position is to get the container position from the camera perspective and then apply that position to the game object, but how to do so?
Ah, your GUI in world space?
In that case:
// Let's find local position of container relative to camera
Vector3 containerWorldPosition = GameObject.FindGameObjectWithTag ("AvatarContainer").GetComponent <RectTransform> ().position;
Vector3 containerLocalPosition = containerWorldPosition - Camera.main.transform.position;
// After that, normalize this vector and place a object on this vector relative to camera
float DesireDistanceFromCamera = 5;
gameobject.transform.position = Camera.main.transform.position + (containerLocalPosition * DesireDistanceFromCamera);
Hi, thanks for your answer, but is not working. Here is an atlas to see what I need and what is the actual result. In the first image you can see the more or less the values that I need. In the second and third image is where the gameobject actually should be to get those values. And in the fourth image is what I get playing with DesireDistanceFromCamera. The object is indeed centered in the container, but because of the camera, which is in perspective mode, it doesn't work. As you can see in the last image, if I play with the z axis of the gameobject it will end up below the camera. I think the equation should consider also the angles, I don't know.