- Home /
Positions in scene editor not same as in-game
Hi, I am trying to create a coin animation in which a value moves up above the coin indicating the value of this coin.
My problem with this is that the positions that I am trying to take from the game objects themselves are very far off. Despite the fact that they are near the center of the screen their x
and y
coords are less than one.
Is there anyway that I can convert the values that I get from gameObject.transform.position
into values that correspond to the coins exact position in-game.
Any and all help is greatly appreciated.
EDIT: Sorry for the lack of information, hopefully I can make it more clear what my problem is.
Ok so I have a coin (blue). Checking the position in the inspector I can see that x is 1.809
and y is 0.075
rounded to 3 decimal places.
Currently the scene looks like:
I have the coins set up so that when they are clicked a their value appears with a sound.
The code for writing the text to the screen is:
void OnGUI() {
if (this.displayText) {
Vector2 pos = gameObject.transform.position;
GUI.Label(new Rect(pos.x, pos.y, 30, 30), "+" + this.value.ToString());
}
}
this.value
is the value of the current coin. Using the positions I get from gameObject
the number ends up in the left corner.
As it seems in the scene view everything is being placed in the center of the screen so the values are skewed quite a lot.
Is there anyway that I can somehow convert the points of the coin object to points that can actually be used in-game to display the text directly above the coin?
A transform's position is its world position. Are you talking about GUI stuff? If yes, it doesn't matter where in the world they are, but where on the screen they are. The long and short of it is that you haven't supplied enough information to actually help you out yet.
Answer by zharik86 · Jan 08, 2014 at 05:46 PM
Yes, you are able to do it. In Unity there is a specific function which will transform object line item in three-dimensional space to a line item on the screen:
Vector3 screenPos = Camera.main.WorldToScreenPoint(gameObject.transform.position);
ScreenPos.x and screenPos.y values - object line item on the camera in pixels. ScreenPos.z value - object depth (usually the point of center of object always undertakes). And further to use:
GUI.Label(new Rect(screenPos.x - 30/2, Screen.height - screenPos.y - 30/2, 30, 30), "+" + this.value.ToString());
I hope it to you will help.
It fixes the problem somewhat but in certain cases the text appears below above and beside the coin
@PurityLake It seems, the text shall be on object center, but I wrote on memory. Try to change value of height of the text as:
Screen.height - screenPos.y + 30/2
But most likely a problem not in it. At you for an output to the screen standard GUI Skin is used. At it for Label text alignment on the upper left corner is set. Try to change value for center.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125822im_/https://answers.unity.com/themes/thub/images/avi.jpg)