- Home /
GUI flickering on screen for certain GUI
In my game I have two different type of GUI running all the time, one for displaying the player's health and the other displaying enemies' health. Those that display the enemies one are fine but the one for the player keeps flickering up and down. They are both using this same script below:
public class GUIFollow : MonoBehaviour {
public float offest;
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 pos = target.position + offest * Vector3.up;
transform.position = Camera.main.WorldToViewportPoint(pos);
}
}
Is this a glitch?
Unless i misunderstood, i don't see any of the GUI code here. Could you include it in your question?
@Professor Snake - considering that he is mapping to viewport space, he is likely using a GUITexture or a GUIText.
I suppose you need to store the position above the player in viewport coordinates to another variable, not the transform.position. Transform is for 3D world coordinates, and what you get with WorldToViewportPoint is not the same type of Vector3. So, store it in another variable, don't override the transform.position with this new value. Then, use this new variable in your OnGUI method to draw the health label on that position.
By the way, it would be better if you would use WorldToScreenPoint method.
Just to make things clear, the script is attach to GUIText and GUITexture.
The problem is now fixed, however using WorldToScreenPoint is not displaying the GUIText and GuiTexture at all. It seem that (using the ViewPortPoint $$anonymous$$ethod) when I checked Apply Root $$anonymous$$otion (from the target transform object) in the Animator it fixed it.
Now I would like to know in a logical sense as in why does the Root $$anonymous$$otion check box will affect it?