using UnityEngine;
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
private RectTransform joyStickBackground;
private RectTransform joyStickHandle;
private Vector2 originalPosition;
void Start()
{
joyStickBackground = GetComponent<RectTransform>();
joyStickHandle = transform.GetChild(0).GetComponent<RectTransform>(); // Предполагается, что handle - первый дочерний объект
originalPosition = joyStickHandle.anchoredPosition;
}
public virtual void OnBeginDrag(PointerEventData eventData)
{
originalPosition = joyStickHandle.anchoredPosition;
}
public virtual void OnDrag(PointerEventData eventData)
{
Vector2 position;
// Проверяем, успешно ли мы получили локальные координаты
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
joyStickBackground,
eventData.position,
eventData.pressEventCamera,
out position))
{
// Считаем новое положение
position -= (Vector2)joyStickBackground.rect.size / 2f; // Исправлено на деление на 2, чтобы центрировать
position = Vector2.ClampMagnitude(position, 75f);
joyStickHandle.anchoredPosition = originalPosition + position;
}
}
public virtual void OnEndDrag(PointerEventData eventData)
{
joyStickHandle.anchoredPosition = originalPosition;
}
}
Сгенерировано чат-ботом «эй бро!»
using UnityEngine.EventSystems;
public class JoyStick : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
private RectTransform joyStickBackground;
private RectTransform joyStickHandle;
private Vector2 originalPosition;
void Start()
{
joyStickBackground = GetComponent<RectTransform>();
joyStickHandle = transform.parent.GetComponent<RectTransform>();
originalPosition = joyStickHandle.anchoredPosition;
}
public virtual void OnBeginDrag(PointerEventData eventData)
{
originalPosition = joyStickHandle.anchoredPosition;
}
public virtual void OnDrag(PointerEventData eventData)
{
Vector2 position;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
joyStickBackground,
eventData.position,
eventData.pressEventCamera,
out position))
{
position -= (Vector2) joyStickBackground.rect.size.delta / 200f;
position = Vector2.ClampMagnitude(position, 75f);
joyStickHandle.anchoredPosition = originalPosition + position;
}
}
public virtual void OnEndDrag(PointerEventData eventData)
{
joyStickHandle.anchoredPosition = originalPosition;
}
} вот ошибка: Assets\Scripts\JoyStick.cs(31,63): error CS1061: 'Vector2' does not contain a definition for 'delta' and no accessible extension method 'delta' accepting a first argument of type 'Vector2' could be found (are you missing a using directive or an assembly reference?)