- Home /
Other
Trying to Fill Up a Bar Slowly but Surely
Hello. I am trying to make a game where the player can utilize an augmented attack that they can use from time to time. They must wait for however much time to pass in order for this bar to fill up completely. Once the bar is completely filled to 100 percent it is then the player is allowed to use this super attack. However I am having trouble over how to scale this bar in its localScale without crashing my unity client (I think I might've caused an infinite loop in some way or form without realizing it and it only happened recently when trying to implement this scaling bar).
Here is the code that I am trying to make this happen but failed to do so:
using UnityEngine;
using System.Collections;
public class AlternateAttackProtocol : MonoBehaviour
{
private static AlternateAttackProtocol instance = null;
public static AlternateAttackProtocol Instance
{
get {return instance; }
}
public Transform alternateAttackDisplay;
public float maxAlternateAttack = 100.0f;
public float currentAlternateAttack;
private float alternateAttackOriginalXScale;
void Start ()
{
alternateAttackOriginalXScale = alternateAttackDisplay.localScale.x;
currentAlternateAttack = 0.0f;
}
// Update is called once per frame
void Update ()
{
//This body of code right here I am trying to make this bar scale accordingly over time.
currentAlternateAttack = currentAlternateAttack + 1;
AlternateAttackTimer();
AlternateAttackInitiated ();
}
void AlternateAttackTimer()
{
while (currentAlternateAttack <= maxAlternateAttack)
{
alternateAttackDisplay.localScale = new Vector3(alternateAttackOriginalXScale * (currentAlternateAttack/maxAlternateAttack), alternateAttackDisplay.localScale.z, alternateAttackDisplay.localScale.z);
}
}
void AlternateAttackInitiated()
{
if (currentAlternateAttack == 100 && Input.GetKeyDown ("Fire2"))
{
currentAlternateAttack = 0.0f;
}
}
}
If there is some better way to do this such as Time.deltaTime please advise. I tried doing that as well but that caused its own way of crashing the client which I'm afraid of doing. Thank you in advance.
Answer by getyour411 · Jan 24, 2015 at 07:20 AM
You might want to go in a different direction; look at the Unity 4.6 new GUI tutorial on UI sliders which also work very well for bars like Health, Mana, Fuel, or Power-Up; an example script interface is shown in the vid towards the end.
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-slider
Thanks for suggesting that but the UI design tutorial didn't seem to have a 3-D look the way I wanted it and were just flat images that changed lengths when you slide the values. Here's an image for an example of what $$anonymous$$e looks like.
I always tended to like things in 3D however when I do get the chance of trying new things I might want to do that in the later future.
UPDATE: Figured out I could do without the while loop since it was already being called in the update method. All I needed was to replace the while loop with an if statement that only works while it is under or equal to 100. Thanks for trying to help however..
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a fill take exactly n seconds to complete 2 Answers