Android joystick doesn't work after 2nd level >.>
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");
}
Answer by Eno-Khaon · Feb 21, 2016 at 10:48 AM
It's a little hard to know for certain, but from what I can see, the script appears to not require any editor-defined variables, so I'll make a few guesses here:
I don't see DontDestroyOnLoad used in your script. Do you currently have multiple scenes for your levels and, if so, is this script present in all of them?
If that's not the problem, are you doing anything unusual when changing scenes/levels? Are you adding or removing content between levels which may result in this script being swept out of the scene at the time?
If that's still not enough, then any further information you can provide which may prove relevant would be great!
Well, It's present in all my scenes but it only works in scene Level1Easy, Level1Hard, Level2Easy and Level2Hard. Other levels it doesn't even react on my finger where it's supposed to move the joystick at the very least. It's like a still image even though it's the exact same prefab from level1Easy where it works. I don't know what's DontDestroyOnLoad but I'm guessing I don't need it since there is most likely a thing in my other levels that would cause the script not to work at all. I will mess around with it for a bit more and see if there's something that should be fixed. By the way, I tried making an entire new script and button and canvas for the new level, it still doesn't work, therefore it must be either the script that has some weird commands in it or a gameObject in those levels >.< I'll see if I can do something
I just tried DontDestroyOnLoad and it didn't fix it :( Also, to make sure everything is properly attached I checked everything while I'm in game mode and sure enough, everything was just like it's supposed to be. The canvas might have something on it so it makes it so I can't touch the canvas objects because there's something above it? I have no idea this is so weird
Answer by RodrigoGomes · May 02, 2016 at 03:02 AM
Man, i am having the same problem with the same script!! There is no reason not to work, i didnt change a thing!! what was your solution??
Your answer
Follow this Question
Related Questions
How can i make my player look at the same direction where its moving? [JOYSTICK ANDROID] 0 Answers
Using the free touchscreen Joystick my code cant detect the joystick 0 Answers
RectTransform Left Right Bottom Top 0 Answers
How can avoid my player to jump twice in the air? 0 Answers
UnityEngine.UI and UnityEngine.EventSystem do not work, what do I do? 1 Answer