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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Float decreasing to -0.99999 not 0

Discussion in 'Scripting' started by LMacalister, Apr 22, 2020.

  1. LMacalister

    LMacalister

    Joined:
    Jul 5, 2017
    Posts:
    9
    I'm making a fuel system and having it decrease from 100 to 0 when the fuel is being used (when the player is moving through input). For some reason my code isn't stopping at zero and instead is going to -0.9999 (recurring). I've tried making the float a range from 0 to 100 but it hasn't fixed it.

    Code (CSharp):
    1. else if (!groundCheck)
    2.                 {
    3.                     if (fuel > 0)
    4.                     {
    5.                         curSpeed = jetPackPower;
    6.                         fuel -= fuelDeduct * Time.deltaTime;
    7.                     }
    If you need more code please ask.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your code doesn't appear to do what you say it should. If "fuel" was 0.00001f the "if" statement on line 3 will resolve to true, and fuel will be subtracted on line 6, I'm guessing in an amount greater than 0.00001f. This will result in negative fuel. If you need fuel to end up at 0f then of course:
    Code (csharp):
    1. else if (!groundCheck)
    2.                 {
    3.                     if (fuel > 0)
    4.                     {
    5.                         curSpeed = jetPackPower;
    6.                         fuel -= fuelDeduct * Time.deltaTime;
    7.                         if (fuel < 0f)
    8.                         {
    9.                            fuel = 0f;
    10.                         }
    11.                     }
     
  3. LMacalister

    LMacalister

    Joined:
    Jul 5, 2017
    Posts:
    9

    I kinda didn't want to do this though as it's hard coding it in. I'm planning on making a regen system for it and I'm worried that having that code will stop the fuel from regenerating
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It is just checking if you've got what is probably an invalid amount of fuel for your game. Alternatively you could do:
    Code (csharp):
    1. if (fuelDeduct * Time.deltaTime > fuel)
    2. {
    3.     fuel = 0f;
    4. }
    5. else
    6. {
    7.     fuel -= fuelDeduct * Time.deltaTime;
    8. }
    But this is effectively the same thing anyways.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    This could also work:

    Code (csharp):
    1. fuel = Mathf.Max(0f, fuel - fuelDeduct * Time.deltaTime);
    Technically it's doing the same thing underneath (it checks if the b < a and returns a if true).
     
    LMacalister and Joe-Censored like this.
  6. LMacalister

    LMacalister

    Joined:
    Jul 5, 2017
    Posts:
    9
    This works! Thank you now just have to do it for the regen as well but hopefully will work the same.