Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Why doesn't my clamp work?

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

  1. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    112
    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:
    112
    Ooh shouldn't have been transform.position.x inside the () in the clamps. Should have been curScreenPoint.x. Case closed.
     
  3. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    1,166
    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. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    2,868
    +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.
     
    SparrowsNest likes this.
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    2,960
    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. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    2,868
    So the important thing is: we are your rubber duck! *sheeepuuu* :D
     
  7. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    112
    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:
    847
    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:
    2,960
    I used a plush penguin until running across a Dalek sale.
     
    RavenOfCode likes this.