K
K
KeysCG2020-06-10 01:21:37
Unity
KeysCG, 2020-06-10 01:21:37

Why does the joystick for android work crookedly?

The problem is that when I press the joystick, it immediately flips to the upper right corner, as if the center was knocked down, but all the centers are in place!
The screenshot shows how when you press anywhere on the joystick, it immediately moves into a corner5ee00b4b5a4c0847384956.png

Here is the joystick code:
using System;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.EventSystems;

namespace TopDownShooter
{
    public class Joystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
    {
        public bool IsTouched;
        public Image JoystickOutCircle;
        public Image Stick;

        public float Horizontal;
        public float Vertical;
        public Vector3 InputDirection;

        private Vector2 _position;

        private void Start()
        {
            if (!JoystickOutCircle) Debug.LogError("Missing JoystickOutCircle image");
            if (!Stick) Debug.LogError("Missing Stick image");
            InputDirection = Vector3.zero;
        }

        public void OnDrag(PointerEventData eventData)
        {
            //Get InputDirection
            RectTransformUtility.ScreenPointToLocalPointInRectangle
            (JoystickOutCircle.rectTransform,
                eventData.position,
                eventData.pressEventCamera,
                out _position);

            var sizeDelta = JoystickOutCircle.rectTransform.sizeDelta;
            _position.x = (_position.x / sizeDelta.x);
            _position.y = (_position.y / sizeDelta.y);

            Horizontal = (Math.Abs(JoystickOutCircle.rectTransform.pivot.x - 1f) > 0.01f)
                ? _position.x * 2 + 1
                : _position.x * 2 - 1;
            Vertical = (Math.Abs(JoystickOutCircle.rectTransform.pivot.y - 1f) > 0.01f)
                ? _position.y * 2 + 1
                : _position.y * 2 - 1;
            InputDirection = new Vector3(Horizontal, Vertical, 0);
            InputDirection = (InputDirection.magnitude > 1) ? InputDirection.normalized : InputDirection;

            //define the area in which joystick can move around
            Stick.rectTransform.anchoredPosition = new Vector3(
                InputDirection.x * (JoystickOutCircle.rectTransform.sizeDelta.x / 3)
                , InputDirection.y * (JoystickOutCircle.rectTransform.sizeDelta.y) / 3);
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            OnDrag(eventData);
            IsTouched = true;
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            InputDirection = Vector3.zero;
            Horizontal = 0;
            Vertical = 0;
            Stick.rectTransform.anchoredPosition = Vector3.zero;
            IsTouched = false;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KeysCG, 2020-06-10
@KeysCG

Damn, as soon as I wrote here, I immediately solved the problem, maybe it will be useful to someone!
It was about the Out rings in the pivot values, made them to 0 and everything became normal5ee00e259f0ea164465100.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question