Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Deplete bar when pressed, re-fill when let go. Stuck!

Discussion in '2D' started by c4mino, Apr 5, 2021.

  1. c4mino

    c4mino

    Joined:
    Nov 26, 2019
    Posts:
    10
    Hey all,

    Been stuck all day - reaching out to see if I've missed something obvious. I'm a beginner so chances are high.

    What I am trying to do is decrease MyNumber when spacebar is held, and increase MyNumber when spacebar is not held. This does mostly work, but rather than MyNumber just switching directions smoothly, it first adds the value of Add or the value of Subtract to MyNumber, THEN it switches directions for counting, so there is a jump in the number by however much the current value of Add or Subtract is.

    Appreciate any help greatly, I've burned a whole day on what probably has an easy solution for the pros.

    Code (CSharp):
    1. public class Counter : MonoBehaviour
    2. {
    3.     public float MyNumber = 10;
    4.     public float Adder = 0;
    5.     public float Subtracter = 0;
    6.  
    7.     float StartSubtractTime;
    8.     float StartAddTime;
    9.     float CurrentTime;
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         CurrentTime = Time.time;
    15.  
    16.         MyNumber = Adder - Subtracter;
    17.  
    18.         if (Input.GetButtonDown("Spacebar"))
    19.         {
    20.             StartSubtractTime = CurrentTime;
    21.         }
    22.  
    23.         if (Input.GetButton("Spacebar"))
    24.         {
    25.             Subtracter = (CurrentTime - StartSubtractTime);
    26.         }
    27.  
    28.  
    29.         if (Input.GetButtonUp("Spacebar"))
    30.         {
    31.             StartAddTime = CurrentTime;
    32.         }
    33.  
    34.         if (!Input.GetButton("Spacebar"))
    35.         {
    36.             Adder = (CurrentTime - StartAddTime);
    37.         }
    38.     }
    39. }
    40.  
    Many thanks!
     
  2. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    178
    I'm not sure what you are trying to accomplish exactly, your code seems overly complicated base on what you described. I would do something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Counter : MonoBehaviour
    6. {
    7.     float myNumber            = 10f;
    8.     float modifierOverTime = 1f;
    9.  
    10.     // Update is called once per frame
    11.     void Update()
    12.     {
    13.         if (Input.GetButtonDown("Spacebar")) {
    14.             myNumber += modifierOverTime * Time.deltaTime;
    15.         } else {
    16.             myNumber -= modifierOverTime * Time.deltaTime;
    17.         }
    18.     }
    19. }
    20.  
    Some other notes:
    - Your variables should not be public unless you have a good reason for it... which is pretty much never the case unless your class is just a data structure
    - Your variables should not start with an upper case
    - You are missing the f at the end of your float
     
    c4mino likes this.
  3. c4mino

    c4mino

    Joined:
    Nov 26, 2019
    Posts:
    10
    Thank you!! Yes it was overly complicated, I based the approach off of a solution to someone else's problem in another thread. I started over using your approach, swapped GetButtonDown for GetButton and it's working great.

    And thank you for the additional notes, I've got a lot to learn. I do know not to leave variables public but I was watching the numbers in the fields in the Inspector as an easy way to debug. I'll work on my naming conventions lol

    Thanks again.
     
  4. Magnesium

    Magnesium

    Joined:
    Sep 14, 2014
    Posts:
    178
    You're welcome.