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

Clamp between two values

Discussion in 'Scripting' started by Fernan7719, Feb 14, 2022.

  1. Fernan7719

    Fernan7719

    Joined:
    Jan 5, 2022
    Posts:
    7
    Hello, I´m having some issues to have a value clamped.
    I have used the function " Mathf.Clamp( value, -2, 2 )", but it doesnt work for my case.
    The range of the input value is between (0-180) and the output value is between (-2,2).
    What am I doing wrong?
    Thanks in advance.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    So you know, saying "doesn't work" does not describe a problem.

    Are you trying to rescale a range of 0 to 180 to a range of -2 to 2? If so, the Clamp method isn't for that. It, well, clamps so any value < -2 it just returns -2 and the same if the value > +2 it just returns +2.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
  4. Fernan7719

    Fernan7719

    Joined:
    Jan 5, 2022
    Posts:
    7
    Yes I´m trying to rescale the value as you say, sorry for my english. When the input value is 0 I have the output to be -2 and when input is 180 the output is 2. That is what I want.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    You do know, that the Clamp method returns the clamped value, it does not (or technically can not) modify the passed in value. No method can do that except for ref parameters.