- Home /
WorldToScreenPoint result of Player Object position with bad Y/Z values
Hi,
I'm building a 3D 3rd person game, using NGUI for UI.
I have several objects the player can interact with, displaying a verb and then a description if the player chooses to interact.
Aesthetically, I want the UILabel to always appear next to the player's position in the world, so I'm using a "WorldToScteenPoint" calculation to translate the player's 3D position into screen-space to be used by the 2D GUI.
The problem is, that every time this calculation is done, the Y position is decrementing and the Z position is incrementing (the Z doesn't make a difference to the appearance, but the Y changes mean that the label soon appears off-screen).
Here is the output of the Debug Prints of the calculated 2D coordinates, and the player's 3D position, just by moving player left-right in and out of trigger:
myScreenPos = (792.8, 302.7, 9.1) 3D Pos = (-0.2, -0.5, -1.4) myScreenPos = (802.4, 240.7, 9.6) 3D Pos = (-0.1, -1.5, -1.4)
myScreenPos = (792.7, 195.7, 10.0) 3D Pos = (-0.2, -2.3, -1.4)
myScreenPos = (793.9, 170.4, 10.2) 3D Pos = (-0.2, -2.7, -1.4)
myScreenPos = (794.1, 151.1, 10.4) 3D Pos = (-0.2, -3.1, -1.4)
myScreenPos = (793.8, 130.5, 10.6) 3D Pos = (-0.2, -3.5, -1.4)
myScreenPos = (800.1, 112.1, 10.8) 3D Pos = (-0.1, -3.9, -1.4)
myScreenPos = (792.5, 93.9, 11.0) 3D Pos = (-0.2, -4.2, -1.4)
myScreenPos = (799.1, 26.4, 11.8) 3D Pos = (-0.1, -5.8, -1.4)
And here is the script that calculates it: (Temporarily, in the Movement script I've created a Static GameObject for the Player, so that I can reference its position in other scripts)
void OnTriggerEnter(Collider myCollider)
{
if (myCollider.CompareTag("Player"))
{
InputManager.instance.ToggleInteractableBool(true, thisScript);
interactLabel.text = textLabel;
Vector3 playerPos = new Vector3(Movement.playerObject.transform.position.x, Movement.playerObject.transform.position.y, Movement.playerObject.transform.position.z);
myScreenPos = Camera.main.WorldToScreenPoint(playerPos);
Debug.Log("myScreenPos = " + myScreenPos + "\n3D Pos = " + playerPos);
interactPanel.transform.localPosition = myScreenPos;
alphaTweener.Toggle();
}
}
Answer by Benproductions1 · Aug 18, 2013 at 11:05 PM
Hello,
The function WorldToScreenPoint
returns the World coordinate in screen space. For purposes of simplification, Unity's GUI (and I'm guessing NGUI too) does not use the standard screen space. In the GUI's screen space, y=0 is at the top of the screen, while in the real screen space y=0 is at the bottom. To fix this you need to do a simple calcuation:
screenPos.y = Screen.height - screenPos.y;
Also note that the Z value represents the distance
from that point to the virtual "screen" in world space. This is sometimes usefull if you want to scale the GUI depending on distance. All this and more can be seen in the documentation if you want any more information ;)
As a last note, on line 9 in your code, you should have:
Vector3 playerPos = Movement.playerObject.transform.position;
And not the horror you currently have ;)
Hope this helps,
Benproductins1
Your answer
Follow this Question
Related Questions
WorldToScreenPoint and enemy target issue 1 Answer
NGUI - Vector 3 Position different from position 1 Answer
How to set my text to real object 2 Answers
Wrapping the visible world? 1 Answer