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.

How do you check if a number is with a range?

Discussion in 'Scripting' started by Wikened, Jan 8, 2012.

  1. Wikened

    Wikened

    Joined:
    Oct 31, 2011
    Posts:
    271
    Ok, so I'm trying to use this script:
    http://www.unifycommunity.com/wiki/index.php?title=Click_To_Move
    But when the player comes close the its target, it slows down and its a pain because I've changed it to run if the transform.position was not equal to the target position. What I want to do is if the players position is within a range of the target position, stop playing the animation?

    How do I check if 1.64 is within 1 and 2?
     
  2. DylanCPL

    DylanCPL

    Joined:
    Jul 14, 2011
    Posts:
    98
    I can't remember the equation, but this should do just as well
    Code (csharp):
    1.  
    2. {
    3.     if (number>smallestNumber&number<largestNumber){
    4.         //do whatever
    5.     }
    6. }
    7.  
     
  3. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
    samson2020 likes this.
  4. Ramor75

    Ramor75

    Joined:
    May 5, 2012
    Posts:
    1
    if (Mathf.clamp(value,min,max)==value){}
    if (Mathf.clamp01(value)==value){} // for ex. raycasted vector/plane intersection :)
     
    Erfo77, arsine- and cyclops1970 like this.
  5. pivotraze

    pivotraze

    Joined:
    Feb 4, 2012
    Posts:
    593
    Boo:
    Code (csharp):
    1.  
    2. if value < maxValue and value > minValue:
    3.     CodeHere
    4.  
     
    Jonimo likes this.
  6. specularpro

    specularpro

    Joined:
    Nov 8, 2017
    Posts:
    6
    He was probably looking for if (FooOrMathf.InRange(val,min,max)) { /*Do junk */ }
     
  7. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    man, don't necro a thread from 2012 and act like it was yesterday
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    ... especially if your proposed solution is worse than the originally proposed

    Code (CSharp):
    1. if (Mathf.clamp(value,min,max)==value){}
    Eight-year necro? Did specularpro perhaps try for a record?
     
    the_real_ijed and orionsyndrome like this.
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    maybe something from pre-Unity times? :)
     
  10. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    The record is beyond 10 years, so his mission failed, I guess.
     
  11. Varaughe

    Varaughe

    Joined:
    Apr 22, 2013
    Posts:
    39
    if you don't know the min and max of that range, rather have this method, m is the value, check if in the range p1,p2 or p2,p1 whichever is the min,max respectively:

    static bool IsValueBetween(float p1, float p2, float m){
    return ((p1 < m) && (m < p2)) || ((p2 < m) && (m < p1));
    }