public class DragJoyStick : MonoBehaviour,IDragHandler,IEndDragHandler {
虚拟摇杆方向上坐标
private Vector3 direction;
设置虚拟摇杆移动最大距离
public float maxDistance;
实现接口,
public void OnDrag(PointerEventData eventData)
{
//鼠标点击摇杆移动
transform.position = Input.mousePosition;
//判断移动距离是否超出最大距离
if (Vector3.Distance(Vector3.zero,transform.localPosition) >maxDistance)
{
//限定摇杆移动距离
direction = transform.localPosition - Vector3.zero;
transform.localPosition = direction.normalized * maxDistance;
}
}
实现接口,虚拟摇杆归位
public void OnEndDrag(PointerEventData eventData)
{
transform.localPosition = Vector3.zero;
}