Search Unity

Math Question, Ratio relative to middle of range

Discussion in 'Scripting' started by FisherM, Sep 23, 2014.

  1. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    I have a max and min value that goes up and down no problem.
    I need a ratio where 1 is the middle value and 0 is the min and the max.
    let's say min is -90 and max is 90 but please take into account I need to be able to plug any min and max in
     
  2. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    I don't really understand what you're trying to get...
    middle = (min + max) / 2, whatever value you have as min and max.

    Or do you want some normalized values (between 0 and 1, 0.5 being the middle) ?
     
  3. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I think an absolute sine will do it.
    I might be wrong, I'm interested in knowing :p
     
  4. BigBGros

    BigBGros

    Joined:
    Aug 30, 2013
    Posts:
    21
    this might work:

    Code (CSharp):
    1. float remap (float min, float max, float value)
    2.     {
    3.         float total = max - min;
    4.         float ratio = value - min;
    5.         float returnValue = 1 - Mathf.Abs(((ratio / total) * 2) - 1);
    6.         return returnValue;
    7.     }
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Or you could just use Mathf.Lerp... isn't that what it's for?
     
  6. FisherM

    FisherM

    Joined:
    Dec 28, 2013
    Posts:
    366
    if (PCM.xRotator.RotationX > 0) {
    vertRatio = 1 - (PCM.xRotator.RotationX / PCM.xRotator.maxX);
    } else if (PCM.xRotator.RotationX < 0) {
    vertRatio = 1 - (PCM.xRotator.RotationX / PCM.xRotator.minX);
    } else {
    vertRatio = 1.0f;
    }
     
  7. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    //t = value from 0.0 to 1.0
    //0.0 = min, 0.5 = max, 1.0 = min
    float range = max - min;
    float value = min + Mathf.PingPong(range * (t * 2), range);