|
I'm getting to grips with most of the 3d Stuff in Unity, I have the guts of a tower defence game written, but I'm having trouble with all the GUI stuff. at the moment I'm just trying to get health-bars to appear above the top of each creep. and I'm not even worried about the shrinking of the healthbar(yet), I just can't get it to appear at the right spot on the screen. Here's my code: The above code gives the following output: [http://twitpic.com/56a0kj][1] (I can't upload to Unity Answers because it just won't take the JPG for some reason) and I modified some of the code to get it to work on my screen: However that's great for my Resolution in the Unity window, but that will all change depending on resolution so I'm looking for some way to get it to appear directly at the point on screen where the enemy is showing... Any ideas ? [1]: http://twitpic.com/56a0kj
(comments are locked)
|
|
This should help you with your issue. } The problem you were facing is that the Camera and screen use the y coordinate in an opposite manner to each other. So your Y coordinate is actually: ok so I used a JS version of your code above ,but the bar appeared far below the item on screen, so I added some Vector3.forward to it - as in the following line and then that seems to work in Unity Editor but when I compile the GUI box doesn't appear ? any ideas ?
Jun 03 '11 at 02:17 PM
TheDavil
I did a quick javascript version: var target: Transform; var healthbarwidth : int = 70; function Update () { } function OnGUI(){ var screenPos : Vector3 = Camera.main.WorldToScreenPoint(target.position); Debug.Log(screenPos.ToString()); GUI.Box(new Rect(screenPos.x - healthbarwidth/2, Screen.height - screenPos.y, 70, 5), "Title"); } This works for me.
Jun 03 '11 at 02:27 PM
RoflHarris
yep you're right. I must have missed something in my code but your last JS version there works great... even after compile - Thanks so much !!!
Jun 03 '11 at 02:54 PM
TheDavil
no problem
Jun 03 '11 at 03:01 PM
RoflHarris
(comments are locked)
|
