I have a guiTexture floating healthbar set up and working great. The healthbar moves with each unit’s position. When a unit is in the center of the screen, the healthbar is above the units head and centered almost perfectly. However, when the unit is commanded away from the center of the screen, the healthbar becomes un-centered more and more as the unit approaches the edge of the screen (same with top and bottom). I believe it is called parallax from the camera? I am using code like this:
Due to my camera being at a 45 degree angle (rts style), I think is what is causing this. Is there a way to smoothly have the healthbar stay centered accurately over the units head as it moves (as in Starcraft 2) even when it is on the edges of the screen, or away from the center? The healthbar also becomes off center when I zoom in and out, for the same reason.
You could use the WorldToViewportPoint to get the objects distance from the center of the screen and use that as a basis for an offset for your health bar. Might not be the best solution, but it will probably do what you want, at least until you find a better solution
Thanks for the reply Elryk. Unfortunately I’m already doing that
I have the screen divided up into about 6 sections, and when a unit enters a given section of the screen (x values), I offset the healthbar. While this kind of works, the player can obviously tell that the healthbar is “teleported” from one location to another, even though its only a small amount. And each offset requires a separate “if” statement in my code, so I have a big block of “if” statements for each section. It seems really bulky as a solution. I might be able to translate it smoothly, but i havn’t figured out how to do that yet.
I was hoping for more of a permanent all-encompassing solution to the problem. I know there must be, I just havn’t found it yet. In theory, I could make about 100 “if” statements dividing the screen x values up into 100 intervals…but that seems terribly time consuming and perhaps unnecessary. Not to mention the code block in the script would be super long. It is an option, however. Thanks for the reply though.
Try it without dividing it up into sections. Find out how far it actually is from the center of the screen and use that distance to actually apply an offset. If you set it up so that you are calculating the offset directly on the position, rather than which section its in, the healthbars offset will change smoothly as the unit moves toward the edge of the screen. I don’t know how much offset you need and this isnt the actual code to get it done (id have to do a little research in the docs), but you can use this as a guideline to what I’m talking about:
PosRelCenter=screenWidth/2 - unitXScreenPosition
Just multiply PosRelCenter by what ever you need to in order to get the proper offset. Then do the same for screenHeight and unitYScreenPosition.
If I’m still not making sense, I’ll write up some psudo-code for you.