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): private Vector3 startpos; // Use this for initialization void Start () { startpos = gameObject.transform.position; } void OnMouseDown () { //offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z)); } void OnMouseDrag() { Vector2 curScreenPoint = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y)); curScreenPoint.x = Mathf.Clamp(transform.position.x, startpos.x, startpos.x + 100); curScreenPoint.y = Mathf.Clamp(transform.position.y, startpos.y, startpos.y); transform.position = curScreenPoint; } void OnMouseUp() { transform.position = startpos; } }
Ooh shouldn't have been transform.position.x inside the () in the clamps. Should have been curScreenPoint.x. Case closed.
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.
+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.