- Home /
GUI meter moves in the wrong direction, how do i fix this?.
i am making a fuel meter GUI for a game, but i've run into a problem, the meter fills from the top down, instead of the other way around.
using UnityEngine;
using System.Collections;
public class FuelGauge : MonoBehaviour
{
public float barDisplay; //current progress
public Vector2 pos = new Vector2(20,40);
public Vector2 size = new Vector2(50,250);
public Texture2D emptyTex;
public Texture2D fullTex;
void OnGUI()
{
//draw the background:
GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
GUI.Box(new Rect(0,0, size.x, size.y), emptyTex);
//draw the filled-in part:
GUI.BeginGroup(new Rect(0,0, size.x * barDisplay, size.y));
GUI.Box(new Rect(0,0, size.x, size.y), fullTex);
GUI.EndGroup();
GUI.EndGroup();
}
void Update()
{
barDisplay = Fuel.currentFuel / Fuel.maxFuel;
}
}
i would fix this myself, but i'm not sure how to fix it to be honest, not sure what's wrong either, i think it's because unity renders rectangles and GUIs from the top right corner, but i don't know how to remedy that.
Answer by Maui-M · Feb 06, 2014 at 09:24 PM
The code below should offset the box by what is not filled in, just replace what you have at line 21-22 from above.
float bar = size.x * barDisplay;
GUI.BeginGroup(new Rect(0,0, size.x, size.y));
GUI.Box(new Rect((size.x-bar,0, size.x * barDisplay, size.y), fullTex);
hrm, this didn't quite work, the fuel meter now just won't increase at all, just stays stuck on empty.
hmm, i got a few errors because of a typo, i fixed that, but the bar does something really strange, ins$$anonymous$$d of just going down like expected, the "full" texture shrinks in size.
using UnityEngine;
using System.Collections;
public class FuelGauge : $$anonymous$$onoBehaviour
{
public float barDisplay; //current progress
public Vector2 pos = new Vector2(20,40);
public Vector2 size = new Vector2(50,250);
public Texture2D emptyTex;
public Texture2D fullTex;
void OnGUI()
{
//draw the background:
GUI.BeginGroup(new Rect(pos.x, pos.y, size.x, size.y));
GUI.Box(new Rect(0,0, size.x, size.y), emptyTex);
//draw the filled-in part:
float bar = size.x * barDisplay;
GUI.BeginGroup(new Rect(0,0, size.x, size.y));
GUI.Box(new Rect(size.x-bar,0, size.x * barDisplay, size.y), fullTex);
GUI.EndGroup();
GUI.EndGroup();
}
void Update()
{
barDisplay = Ball$$anonymous$$ain.currentFuel / Ball$$anonymous$$ain.maxFuel;
}
}
what i'm trying to achieve is like a normal health meter in a game, only ins$$anonymous$$d of being horizontal and filling from the left, it's vertical and fills up from the bottom up.
Your original code was set up modifying the X values so it just seemed like that was the way your camera was set up so X was up/down. If it isn't then use the code as an example and change the Y values ins$$anonymous$$d of X.
i'm sorry if i come off as stupid, i don't get what y vales to change, from reading the code and thinking about it, my only assumptions as for what to change are.
float bar = size.y * barDisplay;
GUI.BeginGroup(new Rect(0,0, size.x, size.y));
GUI.Box(new Rect(size.x-bar,0, size.x * barDisplay, size.y), fullTex);
or
float bar = size.y * barDisplay;
GUI.BeginGroup(new Rect(0,0, size.x, size.y));
GUI.Box(new Rect(size.y-bar,0, size.x * barDisplay, size.y), fullTex);
or
float bar = size.x * barDisplay;
GUI.BeginGroup(new Rect(0,0, size.x, size.y));
GUI.Box(new Rect(size.y-bar,0, size.x * barDisplay, size.y), fullTex);
Your answer
Follow this Question
Related Questions
4.3 GUI 2D Question 1 Answer
Intro, Text scrolling wrong direction (Down not Up) 2 Answers
Can't see GUI layers or sprites 3 Answers
CountDown Camera problem 0 Answers