Вот код персонажа:
public float moveSpeed = 8f;
public Joystick joystick;
private void Update()
{
Vector3 moveVector = (Vector3.right * joystick.Horizontal);
if (moveVector != Vector3.zero)
{
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
}
Джостика:
Vector2 joystickPosition = Vector2.zero;
private Camera cam = new Camera();
void Start()
{
joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
}
public override void OnDrag(PointerEventData eventData)
{
Vector2 direction = eventData.position - joystickPosition;
inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ?
direction.normalized : direction / (background.sizeDelta.x / 2f);
ClampJoystick();
handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
}
public override void OnPointerDown(PointerEventData eventData)
{
OnDrag(eventData);
}
public override void OnPointerUp(PointerEventData eventData)
{
inputVector = Vector2.zero;
handle.anchoredPosition = Vector2.zero;
}
}