Search Unity

Help with lerp? or not lerp

Discussion in 'Scripting' started by dec04, Apr 16, 2019.

  1. dec04

    dec04

    Joined:
    Jan 31, 2013
    Posts:
    9
    Ok. I will be brief.

    May be unity have function what i need.

    I have one variable (call it A) and 2 values, something like 0 and 2000.

    I need function which return 0 if A == 0, return 1 when A == 2000, and return 0.5 when A == 1000.
    Something like mathf.lerp, but reverse direction.
     
  2. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    Its called divide. You want todo it with 2000.
     
    dec04 likes this.
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    As Anders said, divide.

    In your case because your min is 0, you can just divide by 2000.

    If your min was something else, it's still division, just with the min removed:

    Code (csharp):
    1. float PercentageOfMinMax(float value, float min, float max)
    2. {
    3.     return (value - min) / (max - min);
    4. }
     
  4. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Also there's nice function Mathf.Clamp01 wich will help you in case A accidentally will become less than zero or more that 2000.
     
    dec04 likes this.
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    dec04 likes this.
  7. dec04

    dec04

    Joined:
    Jan 31, 2013
    Posts:
    9
    Oh, thanks all for quick reply. Need test it.