- Home /
Player doesn't stop jumping when stop touching the screen[Android]
Hi. I have some code that checks if the player is touching the screen, then if he's touching withing an area, set -Jump to true and if not, set it to false.
This is the code:
if(Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
Vector3 inputGuiPosition = touch.position;
inputGuiPosition.y = Screen.height - inputGuiPosition.y;
if(inputGuiPosition.x > Screen.width / 5)
{
_jump = true;
}
else if(_jump == true)
{
_jump = false;
}
}
}
When I touch the screen in the right side, and let go, the player keeps jumping. When I souch the screen in the left side, while the player is jumping, he stops jumping I'm guessing the problem is something the _jump is only set to false, if the player touches somewhere else on the screen.
If that is the case, how do I fix it?
Thanks for reading
-Frank
Answer by FrimaMickD · Jan 21, 2014 at 08:13 PM
if(Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
Vector3 inputGuiPosition = touch.position;
inputGuiPosition.y = Screen.height - inputGuiPosition.y;
if(inputGuiPosition.x > Screen.width / 5)
{
_jump = true;
}
else if(_jump == true)
{
_jump = false;
}
}
}
else
{
_jump = false;
}
Also, this is a very basic programming question, you should review logic and maybe read some more on programmation before sending tech questions.
You basically answered your own question in text, just not in code!
" I'm guessing the problem is something the _jump is only set to false, if the player touches somewhere else on the screen."
The if(Input.touchCount > 0) will do that...
Good luck with your learning :)
Oh... I moved the
else
{
_jump = false;
}
into the foreach because the player would keep jumping if if kept holding down the finger on another button simultaneously.
I should have copied it ins$$anonymous$$d -_- silly me.
Thanks for the help ;)