Search Unity

Why doesn't my clamp work?

Discussion in 'Scripting' started by ellenblomw, Dec 8, 2018.

  1. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hi,

    I have a drag n drop script that is working. And a "y value" clamping working. Because when I comment out the x value clamping I can drag my object however far I want in x direction. But when I try to clamp my x value I can't move the object at all anymore. Anyone know why?

    Code (CSharp):
    1.  
    2.     private Vector3 startpos;
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.         startpos = gameObject.transform.position;
    7.     }
    8.    
    9.     void OnMouseDown () {
    10.         //offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z));
    11.     }
    12.  
    13.     void OnMouseDrag()
    14.     {
    15.         Vector2 curScreenPoint = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
    16.         curScreenPoint.x = Mathf.Clamp(transform.position.x, startpos.x, startpos.x + 100);
    17.         curScreenPoint.y = Mathf.Clamp(transform.position.y, startpos.y, startpos.y);
    18.         transform.position = curScreenPoint;
    19.     }
    20.  
    21.     void OnMouseUp()
    22.     {
    23.         transform.position = startpos;
    24.     }
    25. }
    26.  
     
  2. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Ooh shouldn't have been transform.position.x inside the () in the clamps. Should have been curScreenPoint.x. Case closed.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I love it when you go nuts over a problem only to see the bug when you post it on the forum, happens to me all the time, lol.
     
  4. +1 it's called rubber duck programming, it helps if you keep something near or on your desk (originally it was a rubber duck, hence the name) and explain to it what are you doing... when you're saying it out loud, you will realize.
    I have a toy dalek, I'm usually co-writing my code with my dalek.
     
    SparrowGS likes this.
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,038
    I have three. My debugging is out of this world (and time-looping, which turns out to be a problem).
     
    Lurking-Ninja likes this.
  6. So the important thing is: we are your rubber duck! *sheeepuuu* :D
     
  7. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    153
    Hahahaha! I will for sure try this. I have a rubber dinosaure, he will be my test subject
     
  8. RavenOfCode

    RavenOfCode

    Joined:
    Apr 5, 2015
    Posts:
    869
    All this time I thought I was crazy lol

    I don't have a duck, but I guess a Yoshi plush counts
     
  9. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,038
    I used a plush penguin until running across a Dalek sale.
     
    RavenOfCode likes this.