Hi,
I’m quite new to Unity (but an experiences Java developer) and I’m currently trying to find out how to calculate some coordinates. What I basically want to achieve is very similar to Unitys slider component (Redirect to... title of new-page). My hierarchy looks like something like this:
- Canvas
- Sliderline (Image)
- SliderKnob (Image)
In a first step I simple want to be able to move the knob using the mouse along a vertical positioned line (Slideline). In case the user drags the mouse above/below the line, the knob should just stop at the top/bottom.
My problem is that I cannot figure out how to correctly convert Input.mousePosition to calculate the new position of the knob. Here is a snippet of what I’m doing currently:
float yOnLine = Input.mousePosition.y;
if (yOnLine > knobMax) {
yOnLine = knobMax;
Debug.Log ("Max position reached");
} else if (yOnLine < knobMin) {
yOnLine = knobMin;
Debug.Log ("Min position reached");
} else {
yOnLine -= knobMin;
}
where knobMax and knobMin are defined as:
knobMax = sliderline.offsetMin.y + sliderline.rect.height;
knobMin = sliderline.offsetMin.y
If I’m using calculations like above, the knob does move - but really weired ;).
Any tips?
This isn’t specifically about sliders, but will show you a better way of handling the drag events, etc.