using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class MobileController : MonoBehaviour,IDragHandler,IPointerUpHandler,IPointerDownHandler { private Image joystickBG; [SerializeField] private Image joystick; private Vector2 inputVector; private void start () { joystickBG = GetComponent(); joystick = transform.GetChild(0).GetComponent(); } public virtual void OnPointerDown(PointerEventData ped) { OnDrag (ped); } public virtual void OnPointerUp(PointerEventData ped) { print ("PointerUP"); inputVector = Vector2.zero; joystick.rectTransform.anchoredPosition = Vector2.zero; } public virtual void OnDrag(PointerEventData ped) { Vector2 pos; if (RectTransformUtility.ScreenPointToLocalPointInRectangle (joystickBG.rectTransform, ped.position, ped.pressEventCamera, out pos)) { pos.x = (pos.x / joystickBG.rectTransform.sizeDelta.x); pos.y = (pos.y / joystickBG.rectTransform.sizeDelta.y); inputVector = new Vector2 (pos.x * 2 - 1, pos.y * 2 - 1); inputVector = (inputVector.magnitude > 1.0 f) ? inputVector.normalized : inputVector; joystick.rectTransform.anchoredPosition = new Vector2(inputVector.x * (joystickBG.rectTransform.sizeDelta.x / 2), inputVector.y * (joystickBG.rectTransform.sizeDelta.y / 2)); } } }