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. Dismiss Notice

Mathf Clamp doesn't clamp!

Discussion in 'Scripting' started by PaxStyle, Jul 19, 2014.

  1. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    Hi to all,
    Someone can explain me why in this script, when the object go to -1 and 1 the object doesn't stops? It should be stop due to mathf.clamp! thank you!

    Code (CSharp):
    1. var move : float = 0.1;
    2. var translation : float;
    3. var pos = transform.position;
    4.  
    5.  
    6. function Start () {
    7. transform.position = pos;
    8. }
    9. function Update () {
    10. if(Input.GetKey(KeyCode.A)) {
    11. translation = Time.deltaTime * move;
    12. transform.position += transform.up * translation;
    13. pos.x =  Mathf.Clamp(transform.position.x, -1f, 1f);
    14. }
    15. }
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Well... maybe if you were doing something with that pos variable...

    By the way, Vector are not a class, but a struct. When you do "pos = transform.position", you create a copy of the original, so modifying "pos" won't affect "transform.position".
     
    chelnok likes this.
  3. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    Ah.. and what can I associate with the "pos"?
     
  4. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    The var translation?

    translation.x = Mathf.Clamp(transform.position.x, -1f, 1f);
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1. transform.position = pos
     
  6. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    You said that i can't use "transform.position = pos" o.o
     
  7. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    He said you can't use pos = transform.position.
    In short words,

    Code (CSharp):
    1. pos = transform.position
    pos in this case stores the coordinates of the gameobject.

    Code (CSharp):
    1. transform.position = pos
    moves the gameobject to pos coordinates.
     
  8. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    But if i don't declare the var pos, the unity editor says ther's an error.
    in this case i put that line over the mathf function, but unity don't recognize the "pos".

    Code (CSharp):
    1. translation = Time.deltaTime * move;
    2. transform.position += transform.up * translation;
    3. transform.position = pos;
    4. pos.x =  Mathf.Clamp(transform.position.x, -1f, 1f);
     
  9. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    you have to clamp pos before you assign the gameobject position, that's why Unity is giving you an error and it doesn't work.
     
  10. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    but how declare pos?
    Oh finally works, i have declared the "var pos : Vector3" at the start, and works fine. the last problem is when i press play, the object change its position just a little, and then works well... why?
     
  11. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    Need more info
     
  12. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    Sure, when i press 'A' the object with the script, moves a little, and then works perfectly.
    if you have not seen, this is the script:

    Code (JavaScript):
    1. var move : float = 0.1;
    2. var translation : float;
    3. var pos : Vector3;
    4.  
    5.  
    6. function Start () {
    7.  
    8. }
    9. function Update () {
    10. if(Input.GetKey(KeyCode.A)) {
    11. transform.position = pos;
    12. pos.x =  Mathf.Clamp(transform.position.x, -1f, 1f);
    13. translation = Time.deltaTime * move;
    14. transform.position += transform.up * translation;
    15.  
    16. }
    17. if(Input.GetKey(KeyCode.S)) {
    18. transform.position = pos;
    19. pos.x =  Mathf.Clamp(transform.position.x, -1f, 1f);
    20. translation = Time.deltaTime * move;
    21. transform.position -= transform.up * translation;
    22.  
    23. }
    24. }
     
  13. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    When you press A for the first time pos has no value but you are telling the object to move to pos.
    It's like calling a friend and telling him to go to null, which doesn't exist.
    You have to invert transform.position and pos.x, like this

    Code (JavaScript):
    1. var move : float = 0.1;
    2. var translation : float;
    3. var pos : Vector3;
    4.  
    5. function Start () {
    6.  
    7. }
    8. function Update () {
    9.     if(Input.GetKey(KeyCode.A)) {
    10.         pos.x =  Mathf.Clamp(transform.position.x, -1f, 1f);
    11.         transform.position = pos;
    12.         translation = Time.deltaTime * move;
    13.         transform.position += transform.up * translation;
    14.     }
    15.        
    16.     if(Input.GetKey(KeyCode.S)) {
    17.         pos.x =  Mathf.Clamp(transform.position.x, -1f, 1f);
    18.         transform.position = pos;
    19.         translation = Time.deltaTime * move;
    20.         transform.position -= transform.up * translation;
    21.     }
    22. }
    Also you should use tabs and blocks of code, it's way easier to read this way :)
     
  14. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    Unfortunately as you said, is worse.. xD
     
  15. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    I also tried to set the start position, but is the same problem

    function Start () {
    transform.position = new Vector3(0.2,-0.3,0.5);
    }
     
  16. PaxStyle

    PaxStyle

    Joined:
    May 22, 2013
    Posts:
    63
    The original position is:
    x: 0.2870008 y: -0.3853648 z: 0.5806122

    after i press A, the position becomes:
    x: 0.06956992 y: 0.150347 z: 0

    why?? o.o
     
  17. TRALLALAL

    TRALLALAL

    Joined:
    Sep 7, 2013
    Posts:
    132
    You only use new in C#.