简易遥杆

MOBA遥杆

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class JoyStick : MonoBehaviour ,IDragHandler,IPointerDownHandler,IPointerUpHandler{

    public RectTransform parent;
    public RectTransform center;
    Camera worldCamera;

    void Start ()
    {
        worldCamera = FindObjectOfType().worldCamera;
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector2 localPoint;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent,Input.mousePosition,worldCamera,out localPoint);

        localPoint = Vector3.ClampMagnitude(localPoint,80);//限制向量locaPoint的模在80以内
        center.anchoredPosition = localPoint;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        Vector2 localPoint;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, Input.mousePosition, worldCamera, out localPoint);

        localPoint = Vector3.ClampMagnitude(localPoint, 80);//限制向量locaPoint的模在80以内
        center.anchoredPosition = localPoint;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        center.anchoredPosition = Vector2.zero;
    }

}

你可能感兴趣的:(简易遥杆)