Answer the question
In order to leave comments, you need to log in
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 corner
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question