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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problem Clamping Value

Discussion in 'Scripting' started by Kalita2127, Mar 27, 2016.

  1. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    Hello guys, I make a script to move along x axis with random y axis. I used Mathf.Clamp to clamp the random value of y axis. Here is the code :
    Code (CSharp):
    1. private void Walk() {
    2.         //transform.position = Vector2.MoveTowards(_currentPosition.position, destination.position, moveSpeed * Time.deltaTime);
    3.         //Mathf.Clamp(transform.position.y, yMin, yMax);
    4.         Debug.Log("Trans y : "+transform.position.y);
    5.         transform.Translate(moveSpeed * Time.deltaTime, Mathf.Clamp(_catchRandomValueY, yMin, yMax) * Time.deltaTime, 0);
    6.         Debug.Log(_catchRandomValueY);
    7.         _currentPosition = transform;
    8.     }
    But the y axis value still exceed the given value of min and max clamp. Can anyone explains why it exceed the value even I used Mathf.Clamp function ?
    Here is the full code : http://pastebin.com/aEX96JKB
    Thank you
     
  2. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    You are clamping the variable _catchRandomValueY correctly but note that using Mathf.Clamp only returns the clamped value therefore it is not actually changing _catchRandomValueY so when you use Debug.Log you seen no difference. To actually change the value of _catchRandomValueY you should do this:
    Code (CSharp):
    1. _catchRandomValueY = Mathf.Clamp(_catchRandomValueY, yMin, yMax) * Time.deltaTime, 0)
     
  3. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    I see, and I think youir code should work correctly.. I've placed it before the transform.Translate but it only change the value slightly, I mean very slighlty..
    I added the conditional statement a moment ago, but now what's the point of using Mathf.Clamp here ? o_O
    Code (CSharp):
    1. private void Walk() {
    2.         //transform.position = Vector2.MoveTowards(_currentPosition.position, destination.position, moveSpeed * Time.deltaTime);
    3.         Mathf.Clamp(transform.position.y, yMin, yMax);
    4.         Debug.Log("Trans y : "+transform.position.y);
    5.         //transform.position += new Vector3(moveSpeed * Time.deltaTime, Mathf.Clamp(_catchRandomValueY, yMin, yMax) * Time.deltaTime, 0);
    6.         transform.Translate(moveSpeed * Time.deltaTime, Mathf.Clamp(_catchRandomValueY, yMin, yMax) * Time.deltaTime, 0);
    7.         _currentPosition = transform;
    8.         if(transform.position.y >= yMax || transform.position.y <= yMin){
    9.             _catchRandomValueY = -_catchRandomValueY;
    10.         }
    11.         Debug.Log(_catchRandomValueY);
    12.     }
    *Edit : so this code
    Code (CSharp):
    1.  transform.Translate(moveSpeed * Time.deltaTime, _catchRandomValueY * Time.deltaTime, 0);
    is actually work perfectly without Mathf.Clamp with conditional statement above
     
    Last edited: Mar 27, 2016
  4. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    I don't quite understand all your changes in the code but this should work:
    Code (CSharp):
    1. private void Walk() {
    2.     Debug.Log("Trans y : "+transform.position.y);
    3.     _catchRandomValueY = Mathf.Clamp(_catchRandomValueY, yMin, yMax) * Time.deltaTime
    4.     transform.Translate(moveSpeed * Time.deltaTime, _catchRandomValueY, 0);
    5.     Debug.Log(_catchRandomValueY);
    6.     _currentPosition = transform;
    7. }
     
  5. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    yeah I already said it before, but that code only changes the value very slightly (about 0.01 - 0.03)
     
  6. BrandonK

    BrandonK

    Joined:
    Sep 18, 2015
    Posts:
    41
    I would image that's because you're multiplying it by Time.deltaTime.
     
  7. Kalita2127

    Kalita2127

    Joined:
    Dec 6, 2014
    Posts:
    265
    hmm.. I don't think so. Time.deltaTime here is to make the movement smooth. I've tested it without Time.deltaTime and the result is the object jumped fast in random y-axis.
    Anyway I think it's already solved by myself by putting the conditional statement written above. Thank you for taking your time to answer my question.