Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Why doesn't my clamp work?

Discussion in 'Scripting' started by ellenblomw, Dec 8, 2018 at 2:15 PM.

  1. ellenblomw

    ellenblomw

    Joined:
    Mar 4, 2018
    Posts:
    92
    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:
    92
    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:
    681
    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. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,756
    +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,950
    I have three. My debugging is out of this world (and time-looping, which turns out to be a problem).
     
    LurkingNinjaDev likes this.
  6. LurkingNinjaDev

    LurkingNinjaDev

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

    ellenblomw

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