Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How to increment/decrement a variable by a decimal number?

Discussion in 'Scripting' started by General Jackson, Aug 2, 2011.

  1. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    Hi everyone, I have a problem. With this Code:
    Code (csharp):
    1. if (throttle < 0)  
    2.   if(Input.GetKeyDown ("-"))
    3.   throttle++;
    It increments throttle variable by 1. That's great and all. But I need to increment it by 0.1
    How do I go about doing this? I cannot figure it out for the life of me!
    Thanks,

    GJ
     
  2. heathCliff

    heathCliff

    Joined:
    Nov 29, 2010
    Posts:
    5
    Code (csharp):
    1.  
    2. throttle += 0.1;
    3.  
     
  3. HarvesteR

    HarvesteR

    Joined:
    May 22, 2009
    Posts:
    499
    how about:

    throttle += 0.1;

    Cheers

    EDIT: Ninja'd!

    Oh, well...

    Most operators will have a self-incrementing version of themselves actually, as += and -=, you can also use *=, /= and %=

    Cheers
     
  4. eTag96

    eTag96

    Joined:
    Oct 24, 2010
    Posts:
    110
    throttle += 0.1f;
     
  5. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    negative countdown, why not to use while instead ?.. or may you are using update function btw, i will add a simplest line :

    throttle += .1;

    i can see this is the first post where all are agreed instantly..
     
  6. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    Code (csharp):
    1. var throttle = 0;
    2. var increment = 0.1;
    3. function Update () {
    4.  
    5.  
    6.  
    7. if (Input.GetKey ("up"))
    8. transform.Rotate(-1,0,0);
    9.  
    10. if (Input.GetKey ("down"))
    11. transform.Rotate(1,0,0);
    12.  
    13. if (Input.GetKey ("left"))
    14. transform.Rotate(0,0,-1.5);
    15.  
    16. if (Input.GetKey ("right"))
    17. transform.Rotate(0,0,1.5);
    18.  
    19. if (throttle > -4)  
    20.   if(Input.GetKeyDown ("="))
    21.   throttle-= 0.1;
    22.  ;
    23.  
    24.  
    25.  
    26. if (throttle < 0)
    27. transform.Translate(0,0,throttle);
    28.  
    29. }
    That is my entire code.
    The Throttle does not work still.
    What could I be doing wrong?
     
  7. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    replace from the second "if" to the last one with "else if", because you are identifying the same condition.. maybe is that ...

    note that you have a semicolon after the "throttle-=0.1; line you must to delete it.. ;)
     
    Last edited: Aug 2, 2011
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,245
    You're typing throttle as an int, so you can't add or subtract decimals from it. Also, that code won't work right, because it's framerate-dependent. You need to use Time.deltaTime. Additionally, it would be simpler if you used GetAxis. (If you do use GetKey, it's better to use KeyCodes rather than strings.)

    --Eric
     
  9. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    Ok how do I change it to a (float?)? when I put 'float throttle' the other parts of the code didnt work.
    Sorry I am still fairly new to JS,
     
  10. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,386
    var throttle = 0.0;
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,245
    Either "var throttle : float = 0", or "var throttle = 0.0".

    It's not doing anything, so it would be better to delete it, but it's not actually necessary. It's a blank line; I wonder if the compiler actually uses some kind of no-op instruction, or if it's ignored entirely?

    --Eric
     
  12. General Jackson

    General Jackson

    Joined:
    Oct 31, 2009
    Posts:
    73
    That did it! Thanks a million!
     
unityunity