Hello,
I am working on an rts where the healthbars “hover” over each unit in the game. I have the healthbars where I want them when each unit is in the center of the screen. However, when I scroll my camera back and forth, left and right, or the unit is commanded to move to the edge of the screen, the healthbar moves too far from the unit (in a not-what-i-want sort of way).
My camera is perspective (I think), looking down like general rts views are (at a 50 degree angle). Is there a way to program to account for this angle in the camera.
Here is my code right now:
function OnGUI () {
//set the gui skin in unity inspector window
GUI.skin = setGUISkin1;
//gets the objects position relative to the camera
screenPosition = Camera.main.WorldToScreenPoint(thisTransform.position);
ylocation = Screen.height - screenPosition.y;
GUI.Box (new Rect(screenPosition.x - (leftBound - 20), ylocation - (topBound + 50), leftBound*1.5, 15), curHealth + "");
}
In short, how do I make the health bar stay in the same position relative to my character? Should I use a texture maybe instead of using the GUI?
are these healthbars in-game objects that just need to face the camera?
look into ‘billboarding’
are these healthbars part of the head’s up display, that need to have a 2d position relative to the player in 3d?
look into camera.WorldToScreenPoint:
Well currently, they are part of the head’s up display that needs to have a 2d position relative to the player in 3d.
I am using the WorldToScreenPoint function currently, and it’s in the general location where I want it to be. However, depending on where the units/agents are on the screen relative to the camera, the healthbars become less centered – to the point that they’re not even close to the unit/agent anymore.
I’m trying to figure out how to compensate for this offset/distortion that happens – I think – because of the transformation from 2d to 3d…I’m not sure though.
I’ve tried transforming the units transform from world space to screen position, and then transforming it again from screen position back to world space, but it didn’t work.