- Home /
GUI objects jitter when following "lerped" Gameobject [SOLVED]
I have a gameobject being lerped/slerped between two positions and quaternions by an AI script on my server. The Healthbar that follows the monster seems to jitter between random positions sometimes even going off screen and coming back.
How do I fix the jitter-like movement of GUI objects "following" a gameobject?
EDIT: A video showing what happens (a bit hard to see because my game runs at 60 fps and the recorder runsa at 32 fps) https://www.mediafire.com/?6hcuccn1787p3s8
Could we please have the code? People can't help, otherwise.
@Lucas$$anonymous$$ars ...sorry about that. Here is the code:
void OnGUI ()
{
//health bar
if(blood > 0 && this.gameObject.GetComponentInChildren<Renderer>().isVisible && disFrom$$anonymous$$ainChar() <= 12f)
{
if(!bloodBarG.enabled)
{
bloodBarG.enabled = true;
bloodBarR.enabled = true;
}
float factor = (float)blood / (float)initBlood;
bloodBarG.transform.position = Camera.main.WorldToViewportPoint(this.gameObject.transform.position);
bloodBarG.transform.localScale = new Vector3(0.002f * factor, 0.0002f, 0);
bloodBarR.transform.position = Camera.main.WorldToViewportPoint(this.gameObject.transform.position);
}
else
{
bloodBarG.enabled = false;
bloodBarR.enabled = false;
}
}
What effect are you trying to achieve with this? A health bar?
What is the object that is "this"?
The GUI object I am referring to is the health bar. "this" is referring to the "monster" gameobject that the health bar follows. Here is a video of what is happening. Sorry for the poor quality but my recorder does not go above 32 fps: https://www.mediafire.com/?6hcuccn1787p3s8
I think I may have found the issue. First, I want to make sure it is the issue I think it is.
Comment out the line: bloodBarG.transform.localScale = new Vector3(0.002f * factor, 0.0002f, 0);
Does it still jitter? If it does, then I will see what else is wrong. If it doesn't, I know an easy way to fix it.
Answer by Victory Dan Greene · Jan 07, 2015 at 05:22 PM
Maybe try setting the position and scale of the health bar only once, on Update() instead of OnGUI()?
OnGUI() can be called multiple times per frame, which might explain the jitter.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
why is my GUI script bugging other GUI scripts? 0 Answers
Using OnGUI to change text in 3D text object. 1 Answer
Smooth Camera transition (linear interpolation) Error 1 Answer