- Home /
Drag an UI slider (no Unity GUI)
Since Unity GUI is only able to show a 2D Interface, I want to make my own 3D GUI within the game. Therefor I created a few Widgets, including a Slider.
I want the user to be able to drag the slider with the mouse. The camera is not fixed, the user is free to move within the game. But the slider will be a child of the camera. I have attached a picture that shows the idea with switches.
Any help is appreciated :)
Answer by Tourist · Dec 12, 2012 at 01:59 PM
Take a look at : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDrag.html
Use a raycast to get the hit point of the mouse on your object and move the slider thumb in that position according to the axis of the slider.
Thank you for your answer. I have implemented your recommended functions. This works well, as long as I don't rotate the camera. For better understanding view this image: http://img826.imageshack.us/img826/84/movetoquestion.jpg
The cube (later the slider) is a child of the camera. I want to move the cube in the direction of the red arrows. To achieve this I currently have to move my mouse up and downwards (green arrows). Any solution ideas are appreciated :).
I am not sure to understand. I m ok with the red arrows but I don't understand why the green arrows are there. Are they perpendicular to the camera forward vector? If you use raycast the green arrows wouldn't be there.
The green arrows symbolize the movement of the raycast that is currently necessary to move the slider. Please note the "Camera Preview" window, the Camera is currently nearly perpendicular to the cube. As far as I understand: the green arrows and the forward vector of the camera are not pependicular.
Take a look at his picture: http://img14.imageshack.us/img14/9504/askquestion.jpg The green arrows in the left image show the movement of the cube. The right image shows the necessary mouse drag movement (red arrows) to achieve the translation of the cube. The small green arrows symbolize the expected dragging movement. Hopefully this helps you understand my problem?
With the last image I can't figure out how you can move the slider thumb to the left, except with moving the mouse on the left part of the screen.
The raycast gives you a point on a plane that is the support of your slider. From that you can project the point on the axis of the slider (the green arrows in the last image). The slider will follow the mouse but will be constraint to the axis.
To project the hit point on the slider axis, I ll suggest you use the forward vector of the camera you project on the plane of the slider or something like that in order to validate the calculation for all camera angles.
Thank you for your answer. The hint about the forward vector of the camera led me to my solution. I had to get the local point of my "interface plane". So I just hat to transform the raycast hit point to a local vector:
var localPos = hitPoint.transform.InverseTransformPoint(hitPoint.point);
Thank you very much for your help. Nevertheless I still have a few other questions about similar to this topic. But I will ask them in a new thread :). Thanks Tourist!