Search code examples
unity-game-engineuislider

Unity - get position of UI Slider Handle


I am working on Unity 4.7 project and need to create shooting on the target. I simulated gunpoint using horizontal and vertical slider moving on the time. When I click the button I need to memorize x and y coordinates of handles and instantiate bullet hole at this point but don't know how to get cords of sliders handle. It is possible to get values but it seems that it doesn't correspond to coordinates. If horizontal slider changes its value for 1, would its handle change x position for 1?


Solution

  • Use this then:

        public static Vector3 GetScreenPositionFromWorldPosition(Vector3 targetPosition)
        {
            Vector3 screenPos = Camera.main.WorldToScreenPoint(targetPosition);
            return screenPos;
        }
    

    Have the reference to Handles of the horizontal and vertical sliders, and use them like:

    Vector3 pos = GetScreenPositionFromWorldPosition(horizontalHandle.transform.position);