- Home /
UI Buttons sometimes not detecting touch
Hi.
I'm making a 2D game and I'm trying to port this to Android. I'm using script below to touch left and right side of screen to move player (with OnPointerUp and Down). Unfortunately sometimes game not recognizing I've touched button. It's totally random. Can someone tell me, what's going on? (sorry for my English btw)
Best Regards.
Script:
public bool moveLeft;
public bool moveRight;
public float buttonsSpeed;
public void Buttons()
{
if (moveLeft && !moveRight)
GetComponent<Rigidbody2D> ().transform.Translate (Vector3.left * buttonsSpeed);
if (moveRight && !moveLeft)
GetComponent<Rigidbody2D> ().transform.Translate (Vector3.right * buttonsSpeed);
}
public void MoveMeLeft()
{
moveLeft = true;
}
public void StopMeLeft()
{
moveLeft = false;
}
public void MoveMeRight()
{
moveRight = true;
}
public void StopMeRight()
{
moveRight = false;
}
As I said before - it's totally random. When I click both buttons game object stop moving at all. Sometimes I can click or hold button but player not moving and i need click once again to move. This is really annoying...
did you use pointer down and pointer up functions?
then you just need a function for if (!moveLeft && !moveRight) then player should not do anything :) .
Answer by PersianKiller · Sep 09, 2017 at 12:48 PM
so your player cant stop moving,his going to left or right;
just change your code to it
public void MoveMeLeft()
{
moveLeft = true;
moveRight = false;
}
public void MoveMeRight()
{
moveLeft = false;
moveRight = true;
}
and you don't need a pointer up function anylonger if your character can't stop moving. and if you want your character to stop just in pointer up function call stop moving function hope this helps.
Thanks for this script, now movement is better but still the problem isn't solved. After maybe 30 sec of gameplay, my player not moving when i click/hold right or left button. I need to click/hold the button one more time... :/ It's looks like sometimes phone/game not recognizing my touch...
np dude ,another question,your character should move when you are pushing the button ? or if you click it goes for ever?
If i hold / click button should move, when release should stop. I think script is ok but maybe there is something wrong with unity setting (maybe e.g. something in EventSystem ?)