КОД:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MobelController : MonoBehaviour,IDragHandler,IPointerUpHandler, IPointerDownHandler {
private Image joysticBG;
[SerializeField]
private Image joystic;
private Vector2 inputVector;
private void Start()
{
joysticBG = GetComponent<Image>();
joystic = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector2.zero;
joystic.rectTransform.anchoredPosition = Vector2.zero;
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(joysticBG.rectTransform,ped.position.pressEventCamera, out pos))
{
pos.x = (pos.x / joysticBG.rectTransform.sizeDelta.x);
pos.y = (pos.y / joysticBG.rectTransform.sizeDelta.y);
inputVector = new Vector2(pos.x * 2 - 1, pos.y * 2 - 1);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
joystic.rectTransform.anchoredPosition = new Vector2(inputVector * (joysticBG.rectTransform.sizeDelta.x / 2), inputVector * (joysticBG.rectTransform.sizeDelta.y / 2));
}
}
}
ОШИБКИ В КОНСОЛИ:
error CS1502: The best overloaded method match for `UnityEngine.Vector2.Vector2(float, float)' has some invalid arguments
error CS1061: Type `UnityEngine.Vector2' does not contain a definition for `pressEventCamera' and no extension method `pressEventCamera' of type `UnityEngine.Vector2' could be found. Are you missing an assembly reference?
error CS1501: No overload for method `ScreenPointToLocalPointInRectangle' takes `3' arguments
Argument `#1' cannot convert `UnityEngine.Vector2' expression to type `float'