- Home /
Question is off-topic or not relevant
Golf Gamelike power bar
Hi, I have done a power system and it's working fine I think, but I don't know how to make the GUI part, I want the bart to be kinda like in this game: http://www.ebark.se/UNITY/golf_1.html (different visuals though, but vertical, also the bar goes up and down instead of just stopping at the top), can anyone help me with this GUI task?I use c# btw
Thanks in advace
Answer by RyanZimmerman87 · Nov 10, 2013 at 07:50 PM
I'll try to do a quick example for you but you'll need to customize the timings with the math to perfect this. This is example for general functionality:
//style to easily format your GUI displays
public GUIStyle swingStyle;
public Texture2D emptyBarTexture;
//texture to display at swings peak
public Texture2D fullBarTexture;
public Texture2D currentSwingPowerTexture;
//vector 2 size and position for your bar
Vector2 vectorSwingPosition = new Vector2 (200, 240);
Vector2 vectorSwingSize = new Vector2 (50, 300);
//floats for your bar's display %
float currentSwingPower;
float maxSwingPower;
float swingCompletePower;
//the float for the percentage currentSwingPower/maxSwingPower
float swingDisplayFloat;
//swing currently active bool
bool swingActiveBool;
//bool for if power bar going up or down
bool swingUpBool;
void OnGUI()
{
if (swingActiveBool == false)
{
//empty container group
GUI.BeginGroup(new Rect (vectorSwingPosition.x, vectorSwingPosition.y, vectorSwingSize.x, vectorSwingSize.y));
//empty container texture
GUI.Box(new Rect(0, 0, objectiveSize.x, objectiveSize.y), emptyBarTexture, swingStyle);
return;
}
else if (SwingActiveBool == true)
{
//empty box for background
GUI.BeginGroup(new Rect (vectorSwingPosition.x, vectorSwingPosition.y, vectorSwingSize.x, vectorSwingSize.y));
GUI.Box(new Rect(0, 0, objectiveSize.x, objectiveSize.y), emptyBarTexture, swingStyle);
//calculate how big the power bar will be
GUI.BeginGroup(new Rect (0, (vectorSwingSize.y - (vectorSwingSize.y * swingDisplayFloat)), vectorSwingSize.x, vectorSwingSize.y * swingDisplayFloat));
//draw power bar texture
GUI.Box(new Rect (0, -vectorSwingSize.y + (vectorSwingSize.y * swingDisplayFloat), vectorSwingSize.x, vectorSwingSize.y), currentSwingPowerTexture, swingStyle);
GUI.EndGroup();
GUI.EndGroup ();
}
}
void Update()
{
//if your key press to start swing
//substitute your desired button
if (Input.GetButtonDown ("Fire1"))
{
if (swingActiveBool == false)
{
swingActiveBool = true;
swingUpBool = true;
}
else if (swingActiveBool == true)
{
if (swingUpBool == true)
{
maxSwingPower = currentSwingPower;
swingUpBool = false;
}
else if (swingUpBool == false)
{
swingCompletePower = currentSwingPower;
swingActiveBool = false;
}
//calculate bar Percentage
swingDisplayFloat = currentSwingPower/maxSwingPower;
}
}
//do your bar calculations at Fixed TimeStep
void FixedUpdate()
{
if (swingActiveBool == false)
{
return;
}
else if (swingActiveBool == true)
{
if (swingUpBool == true)
{
++currentSwingPower;
}
else if (swingUpBool == false)
{
--currentSwingPower;
}
}
}
Ugh sorry for the formatting problems don't really have time to clean all that up. I really seem to make a mess of my code examples when they are big not sure if the system just hard to use or something about the way I format my code throws it off big time :(
Well that's the basic example of how to draw a power bar within a container bar based on what percentage of the bar should be displayed.
This example is missing quite a bit of stuff you'll need for the final version but at least you'll be able to get started more easily now.
Things you'll still need just in case it's not clear what this example includes:
1) Way to display the max power texture for the peak of swing
2) Way to display the finish power texture for end of swing
3) Way to make the bar persist a little bit after the swing finishes instead of instantly clearing the bar.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
NullreferenceException, BeginScrollView style change 3 Answers
Scaled GUI 1 Answer
Create a Button Scrollview 0 Answers