So I just made this script for android joystick, works perfectly tested and approved on my phone. But there is one thing though – The joystick magically stops working after 2nd level, even though I never changed a single thing. To make sure I haven’t changed anything, I made another script, the same as the previous one. Still doesn’t work ._. Weirdest thing is that it won’t even move when I make a script that would literally only move the joystick when I click on it. Is something altering it in my scene or is it just aliens? Can someone please help me with this it’s driving me crazy >.< Here’s the script if it helps at all
private Image bgImg;
private Image joystickImg;
private Vector3 inputVector;
private void Start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImg.rectTransform
, ped.position
, ped.pressEventCamera
, out pos))
{
pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgImg.rectTransform.sizeDelta.y);
inputVector = new Vector3(pos.x * 2 + 1, 0, pos.y * 2 - 1);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
// Move Joystick IMG
joystickImg.rectTransform.anchoredPosition = new Vector3(inputVector.x * (bgImg.rectTransform.sizeDelta.x / 2) , inputVector.z * (bgImg.rectTransform.sizeDelta.y / 2));
}
}
public float Horizontal()
{
if (inputVector.x != 0)
return inputVector.x;
else
return Input.GetAxis("Horizontal");
}
public float Vertical()
{
if (inputVector.x != 0)
return inputVector.z;
else
return Input.GetAxis("Vertical");
}