- Home /
Set dragging boundaries for a self-made slider
I try to develop a 3D GUI (since the Unity GUI is only able to show 2D elements). Hence I have attached a plane and two sliders as a child to my main camera. The sliders represent values from 0% to 100%. Therefor they have to have limits. The image below shows the setup:
Thanks to Tourist (see here) I am able to drag my sliders vertically by mouse. Currently it is possible to drag the slider up to the edge of the dark gray plane. But I want them to stay in the light gray frame.
My current solution is not a really good one. I have two variables in local space:
var bottomBound = -0.351691; (self measured)
var topBound = -1.135893; (self measured)
If the next position of the sliders would exceed these bounds, dragging would be suppressed. I do the measuring by shooting a raycast and printing the local coordinates of the vector.
var localPos = hitPoint.transform.InverseTransformPoint(hitPoint.point);
Debug.Log("(x,y,z) = (" + localPos.x + "," + localPos.y + "," + localPos.z + ")");
But there is one major problem with this solution. Even if my measurements were pretty good, the slider still can move beyond the edges of the light gray area. I suspect that this is due to the position of the pivot point.
What can I do to keep the slider in the wished area?
PS: Can I get the width and height of the sliders?
Answer by jeeyoh · Apr 01, 2013 at 09:03 AM
you can always try using Mathf.Clamp to limit their movements
Your answer
![](https://koobas.hobune.stream/wayback/20220613085517im_/https://answers.unity.com/themes/thub/images/avi.jpg)