- Home /
NGUI positioning over NPC
Hi.
As the question says, I'm trying to position a NGUI object over a NPC.
This is the code that I'm using to do it:
private GameObject CreateOrderBalloon()
{
GameObject balloon = GameObject.Instantiate( _orderPrefab ) as GameObject;
balloon.transform.parent = NGUITools.FindCameraForLayer( LayerMask.NameToLayer( "GUI2D" ) ).transform.parent;
balloon.GetComponent<GUIOrderBalloon>().SetOrder( _currentOrder );
Vector3 gui2dPos = Camera.main.WorldToScreenPoint( transform.position );
Debug.Log(transform.position);
gui2dPos.x -= (Screen.width / 2.0f);
gui2dPos.y -= (Screen.height / 2.0f);
gui2dPos.z = 0;
Debug.Log(gui2dPos);
balloon.transform.position = Camera.main.ScreenToWorldPoint(gui2dPos);
balloon.transform.localRotation = Quaternion.identity;
balloon.transform.localScale = Vector3.one;
return balloon;
}
However, no matter what's my NPC position, the balloon is always instantiated at Vector3.zero.
For example, this is what the Debug.Log prints:
Debug.Log(transform.position): -4.5, 0.0, 0.0
Debug.Log(gui2dPos): -133.4, 0.0, 0.0
And that is the position of the instantiated NGUI balloon:
P: 0, 0, 0
R: 0, 0, 0
S: 1, 1, 1
Here is the whole scene:
As can be seen, the ballon is in the middle of the scene, not in the same position of the NPC at the stall. I don't know what is wrong. I've been struggling with this problem for months now. :S
Thank you.
PS: As an alternative, I created a UIGrid and added each item there with the following code:
private GameObject CreateOrderBalloon()
{
GameObject balloon = GameObject.Instantiate( _orderPrefab ) as GameObject;
balloon.GetComponent<GUIOrderBalloon>().SetOrder( _currentOrder );
UIGrid ordersGrid = GameObject.Find("Orders").GetComponent<UIGrid>();
ordersGrid.AddChild( balloon.transform );
ordersGrid.Reposition();
balloon.transform.localScale = Vector3.one;
balloon.transform.localPosition = Vector3.zero;
balloon.transform.localRotation = Quaternion.identity;
return balloon;
}
This worked properly. So, I don't think I changing these items positioning somewhere else after the Debug.Log.
Your answer
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Object following other object (Parent-like) 2 Answers
Following mouse 2 Answers
Camera position not updating 1 Answer
can i let the sphere follow a point(x.y. or.z) on plane? 1 Answer