实现

  1. 直接使用系统自带圆形控件图标
  2. 编写脚本, 实现UGUI拖拽事件
  3. 将多拽范围限定于给定半径和圆心的圆内
  4. 计算出等同于Input.GetAxis()的值,直接控制被控制物体

代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; public class MyJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IEndDragHandler
{
public float Radius = 100f;
private Vector2 center;
private RectTransform rectTransform;
// Use this for initialization
void Start()
{
rectTransform = GetComponent<RectTransform>();
center = rectTransform.position;
} public void OnDrag(PointerEventData eventData)
{
Vector2 vPos = eventData.position;
var distance = Vector2.Distance(vPos, center);
if (distance > Radius)
{
vPos = (vPos - center).normalized * Radius + center;
}
rectTransform.position = new Vector3(vPos.x, vPos.y, ); // if (TTMove)
// {
// var npos = (vPos - center) / Radius;
// TTMove.forwardInput = npos.y;
// TTMove.turnInput = npos.x;
// }
} public void OnPointerUp(PointerEventData eventData)
{
rectTransform.position = new Vector3(center.x, center.y, );
// if (TTMove)
// TTMove.forwardInput = TTMove.turnInput = 0;
} public void OnEndDrag(PointerEventData eventData)
{
rectTransform.position = new Vector3(center.x, center.y, );
// if (TTMove)
// TTMove.forwardInput = TTMove.turnInput = 0;
}
}

把脚本挂到
UGUI简易摇杆-LMLPHP

<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">

 
 
 
 
05-11 23:03