Question by 
               avatarair1 · May 13, 2016 at 04:28 AM · 
                c#mathfmathf.clamp  
              
 
              Mathf.Clamp does not work at all.
I am using C# and I am trying to clamp a velocity value that will increase when I press down on my accelerate button. The code is as follows:
 if (Input.GetKey(KeyCode.W))
         {
             currentVelocity += forwardAccelerationRate * Time.deltaTime;
             maxVelocity = 50.0f;
         }
         else if (Input.GetKey(KeyCode.S))
         {
             currentVelocity += reverseAccelerationRate * Time.deltaTime;
             maxVelocity = 10.0f;
         }
         else
         {
             currentVelocity -= deccelerationRate * Time.deltaTime;
         }
 
         currentVelocity = Mathf.Clamp(currentVelocity, initialVelocity, maxVelocity);
         Debug.Log(Mathf.Clamp(currentVelocity, initialVelocity, maxVelocity));
 
               where
 initialVelocity = 1.0f;
 currentVelocity = 0.0f;
 maxVelocity = 5.0f;
 forwardAccelerationRate = 2.0f;
 reverseAccelerationRate = 0.5f;
 deccelerationRate = 0.7f;
 
               My currentVelocity variable just continues to increase no matter what.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Naphier · May 13, 2016 at 06:01 AM
Mathf.Clamp works 100%. What other code do you have that is affecting the variables? I'd suggest
  Debug.LogFormat("currentVelocity: {0}   initialVelocity: {1}    maxVelocity: {2}", currentVelocity, initialVelocity, maxVelocity);
 
               for a closer inspection of all of the variables.
I looked at my variables and my max was being set to a higher value which i totally forgot about. Thanks for your help
Your answer
 
             Follow this Question
Related Questions
Save object position after drag? 0 Answers
Please help me! Correct script) , 0 Answers
Speed exceeds the clamped value by 0.3? 1 Answer