Unity and Cocos stay as parallel schemes; apps/minigame ships the Godot 4.7 WeChat pack for DevTools. Co-authored-by: Cursor <cursoragent@cursor.com>
61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace Nomadro.Minigame
|
|
{
|
|
/// <summary>
|
|
/// UI joystick (left) — assign to a UI Image with EventTrigger, or use OnDrag handlers.
|
|
/// </summary>
|
|
public class VirtualJoystick : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
|
|
{
|
|
public PlayerMotor motor;
|
|
public RectTransform knob;
|
|
public float maxRadius = 72f;
|
|
public System.Action onInteract;
|
|
|
|
RectTransform _root;
|
|
Vector2 _origin;
|
|
bool _active;
|
|
|
|
void Awake()
|
|
{
|
|
_root = transform as RectTransform;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.E) || Input.GetKeyDown(KeyCode.Space))
|
|
onInteract?.Invoke();
|
|
}
|
|
|
|
public void OnInteractButton()
|
|
{
|
|
onInteract?.Invoke();
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
_active = true;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
|
_root, eventData.position, eventData.pressEventCamera, out _origin);
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
if (!_active || motor == null) return;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
|
_root, eventData.position, eventData.pressEventCamera, out var local);
|
|
var delta = Vector2.ClampMagnitude(local - _origin, maxRadius);
|
|
if (knob != null) knob.anchoredPosition = delta;
|
|
motor.SetMoveInput(new Vector2(delta.x / maxRadius, delta.y / maxRadius));
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
_active = false;
|
|
if (knob != null) knob.anchoredPosition = Vector2.zero;
|
|
motor?.SetMoveInput(Vector2.zero);
|
|
}
|
|
}
|
|
}
|