1

I'm trying to do something like the old GTA style money system like in Gta vice city or san andreas. So when you add or obtain money, the number doesn't just jump to the result. It slowly increments till the value added is done.

I want to do this by clicking buttons, so one button will add 100 dollars and other will subtract 100 dollars and so on.

The buttons don't seem to be playing nice with update and Time.deltatime.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Pikachu
  • 59
  • 1
  • 8

1 Answers1

1

To slowly increment a number over the time, you can do something like this:

    public float money = 100;
    public int moneyPerSecond = 25;
    public int moneyToReach = 100;
    bool addingMoney = false;

    private void Update()
    {
        if (addingMoney)
        {
            if (money < moneyToReach)
            {
                money += moneyPerSecond * Time.deltaTime;
            }
            else { addingMoney = false; money = Mathf.RoundToInt(money); }
        }
    }

    public void addMoney()
    {
        moneyToReach += 100;
        addingMoney = true;
    }

Cardstdani
  • 4,999
  • 3
  • 12
  • 31
  • 1
    Is there any reason to add moneyToReach to 100 by default? like can I put that to 0 and then use my method calls to do things like += 100, 200 etc? – Pikachu Jul 12 '20 at 08:56
  • 1
    Yes, you can set the money and moneyToReach to 0 by default, and then, add the quantity that you want – Cardstdani Jul 12 '20 at 09:21