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.