Skip to content

Commit

Permalink
[update] 不需要贴合手指的改善,此改善不成熟且与我原神操作体验不一致
Browse files Browse the repository at this point in the history
  • Loading branch information
Bian-Sh committed Nov 8, 2024
1 parent e53d325 commit 73a94c7
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions Packages/Joystick/Runtime/Joystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class Joystick : MonoBehaviour, IPointerDownHandler, IDragHandler, IPoint
[SerializeField] bool showDirectionArrow = true; // 是否展示指向器
[Space(8)]
public JoystickEvent OnValueChanged = new JoystickEvent(); //事件 : 摇杆被 拖拽时
private Canvas rootCanvas;


#region Property
public bool IsDraging { get { return fingerId != int.MinValue; } } //摇杆拖拽状态
public bool ShowDirectionArrow { get => showDirectionArrow; set => showDirectionArrow = value; } // 是否展示指向器
Expand All @@ -35,12 +34,7 @@ public bool IsDynamic //运行时代码配置摇杆是否为动态摇杆
#endregion

#region MonoBehaviour functions
void Start()
{
rootCanvas = transform.root.GetComponent<Canvas>();
backGroundOriginLocalPostion = backGround.localPosition;
}

void Start() => backGroundOriginLocalPostion = backGround.localPosition;
void Update() => OnValueChanged.Invoke(knob.localPosition / maxRadius); //fixedupdate 为物理更新,摇杆操作放在常规 update 就好
void OnDisable() => RestJoystick(); //意外被 Disable 各单位需要被重置
void OnValidate() => ConfigJoystick(); //Inspector 发生改变,各单位需要重新配置,编辑器有效
Expand Down Expand Up @@ -70,15 +64,7 @@ void IDragHandler.OnDrag(PointerEventData eventData)
x = (activatedAxis == Direction.Both || activatedAxis == Direction.Horizontal) ? (direction.normalized * radius).x : 0, //确认是否激活水平轴向
y = (activatedAxis == Direction.Both || activatedAxis == Direction.Vertical) ? (direction.normalized * radius).y : 0 //确认是否激活垂直轴向,激活就搞事情
};
// knob.localPosition = localPosition; //更新 Handle 位置
Vector2 anchoredPosition = eventData.delta / rootCanvas.scaleFactor;
((RectTransform)knob).anchoredPosition += anchoredPosition;

// 限制摇杆范围
float knobDistance = Mathf.Clamp(Vector2.Distance(Vector2.zero, ((RectTransform)knob).anchoredPosition), 0, maxRadius); // 获取摇杆当前位置到中心的距离,并限制在最大范围内
Vector2 normalizedPosition = ((RectTransform)knob).anchoredPosition.normalized; // 获取摇杆当前位置的单位向量
((RectTransform)knob).anchoredPosition = normalizedPosition * knobDistance; // 将摇杆位置限制在最大范围内

knob.localPosition = localPosition; //更新 Handle 位置
if (showDirectionArrow)
{
if (!arrow.gameObject.activeInHierarchy) arrow.gameObject.SetActive(true);
Expand Down

0 comments on commit 73a94c7

Please sign in to comment.