I’m fairly new to Game Development, although i do have basic experience in programming. My problem is as follows:
I’m writing a simple prototype script that slowly increases a float while an OnGUI repeatbutton is pressed. I got it to increase, but what I’m wanting to do is once the button is released, it captures the value the number has reached to, and then slowly decreases it back down to its original value.
The problem I’m having is that when I put in the code to decrease the value, the script seems to want to do both at the same time, which causes it to stall, and the value doesn’t increase when the button is pressed.
My script so far looks as follows, without the decreasing code:
using UnityEngine;
using System.Collections;
public class IncreaseValuePrototype : MonoBehaviour {
public float value, minValue;
public float totalValue;
// Use this for initialization
void Start () {
value = 8;
totalValue = value * 10;
minValue = value;
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
GUI.Label (new Rect(10, 10, 150, 25), "Value: " + (int)value);
GUI.Label (new Rect (10, 25, 150, 25), "Max Value: " + totalValue);
if ((GUI.RepeatButton (new Rect (25, 50, 100, 20), "Increase Value"))) {
value = Mathf.MoveTowards (value, totalValue, 20f * Time.deltaTime);
}
}
}
I’ve left out the code to decrease the value, but essentially I want it to do the reverse of the above once the button is no longer being pressed.
I hope my goal makes sense, and that someone here will be able to guide me towards the best method to use for this approach.