- Home /
Orthographic objects follow in perspective
I'm using EZ GUI for HUD and I would like some elements like a health-bar to follow my hero in the scene, but I don't want those HUD elements to have perspective, I don't want them to take a larger amount of the screen as my hero gets closer to the camera. So I did the following:
Vector3 screenpos = cam.WorldToScreenPoint(new Vector3(myParent.position.x, myParent.position.y+1, myParent.position.z));
transform.position = GUICam.ScreenToWorldPoint(screenpos);
Where cam is the main-perspective camera and GUICam is an orthographic camera for HUD. That works, the HUD follows my hero as it moves, but it shakes on the screen for every movement, it happens when used on Update() and LateUpdate(). So I tried OnGUI() for a faster update rate and it shows now smooth and perfect.
The point is, as a mobile application, I don't want to use OnGUI() functions on my project to save performance. Is there some way I can optimize that?
Answer by equalsequals · Aug 11, 2011 at 09:00 PM
I believe the shaking you are referring to could be the fact that your WorldToScreenPoint is taking a Vector3 and outputting a Vector3, which is made of floats. In a vector environment this would be okay, but in a raster environment you get shakes and slight distortions when it comes to fractional pixels.
It might be beneficial to use LateUpdate to take advantage of the execution order, and ensure that the Vector3 you are using to assign the screenspace position of your health bar is comprised of only floats. You can do this by doing something like this:
Vector3 screenpos = cam.WorldToScreenPoint(new Vector3(myParent.position.x, myParent.position.y+1, myParent.position.z));
transform.position = GUICam.ScreenToWorldPoint(new Vector3((int) screenpos.x, (int) screenpos.y, (int) screenpos.z)); //typecasting a float to an int automatically drops anything after the decimal.
Or this is essentially the same thing:
transform.position = GUICam.ScreenToWorldPoint(new Vector3(Mathf.Round(screenpos.x), Mathf.Round(screenpos.y), Mathf.Round(screenpos.z))); //You could also substitute Round with Ceil() or Floor() if you wanted fine-tuned control of this.
If that doesn't do it, I have another idea what it could be - I also do not use EZGUI so I am making speculations on some of it's functionality as to how it handles certain things.
Hope that helps.
==
Answer by Julian Witte · Aug 11, 2011 at 09:32 PM
Hi, equalsequals,
EZ GUI generates 3D meshes to draw interface elements, thats the reason why they are affected by perspective cameras.
In that case, I can't cast those values to int, otherwise that will distort the hero (myParent) position in the world received by the camera. That actually stops the shaking, the hud won't follow my hero smoothly, it jumps to the nearest non-decimal position points in the world.
I just figured out that, the shaking is caused by the camera smooth follow behaviour, it shakes as the camera smoothly adjust its position to follow the hero. I've got a better result using a Vector3.Lerp to update the HUD position, so the shaking is no longer noticeable.
Thanks!
Your answer
Follow this Question
Related Questions
Draw orthographic visuals (e.g. health bar) in perspective scene? 4 Answers
Using a perspective camera can an object act as if viewed by orthographic camera? 1 Answer
Camera 2D RPG in perspective 1 Answer
fluent animation from orthographic to perspective projection 2 Answers
Click and Drag Camera only works in Orthographic camera 1 Answer