Problem with simple Character movement on Android (Touch/GetMouseButtonDown)
Hello, i have a problem with GetMouseButtonDown and Android Touch. i have this code, which works fine on Pc. (C# by the way)
if (Input.GetMouseButtonDown(0) && !isDead)
{
playing = true;
score++;
scoreText.text = score.ToString();
speed += 0.08f;
if (dir == Vector3.forward)
{
dir = Vector3.left;
}
else
{
dir = Vector3.forward;
}
}
if (Input.GetMouseButtonDown(1) && !isDead)
{
playing = true;
score++;
scoreText.text = score.ToString();
speed += 0.08f;
//Switches the players direction every time we click ont he screen or mouse
if (dir == Vector3.forward)
{
dir = Vector3.right;
}
else
{
dir = Vector3.forward;
}
}
which does:
When you left click, he moves forward, if you left click again he goes to the left. if you left click again he goes forward etc.
if you right click he moves forward, if you right click again he goes to the right. if you right click again he goes forward etc.
but when i try it on my Android phone, it only works with GetMouseButtonDown(0) and not (1) so every time i click on the screen, no matter where, the character goes left, if you click again he goes forward.
is there any way to make mousebuttondown(0) half of the screen and mousebuttondown(1) the other half? then it would work.
i tried to do this:
foreach (Touch touch in Input.touches)
{
if (touch.position.x < Screen.width / 2)
{
if (Input.GetMouseButtonDown (0) && !isDead) {
playing = true;
score++;
scoreText.text = score.ToString ();
speed += 0.08f;
//Switches the players direction every time we click ont he screen or mouse
if (dir == Vector3.forward) {
dir = Vector3.left;
} else {
dir = Vector3.forward;
}
}
}
else if (touch.position.x > Screen.width / 2)
{
if (Input.GetMouseButtonDown (1) && !isDead)
{
playing = true;
score++;
scoreText.text = score.ToString ();
speed += 0.08f;
//Switches the players direction every time we click ont he screen or mouse
if (dir == Vector3.forward)
{
dir = Vector3.right;
}
else
{
dir = Vector3.forward;
}
}
}
}
but it still dont work correctly. I dont want the player to be able to "hold" his finger on the screen, it should only register one click like on the mouse, untill the finger is lifted and the next touch is registered.
Im still bad at Unity so im sorry if this is a stupid question or hard to understand.
Any tips would be appreciated. Tekar
Answer by tekkar · Feb 13, 2016 at 11:47 PM
fixed it by making Input.GetMouseButtonDown (1) to (0) on the other half of the screen aswell
Your answer
Follow this Question
Related Questions
UNITY 2D ANDROID DEVICE TOUCH PROBLEM 0 Answers
Android Controls 1 Answer
Jump by touch Unity C# 0 Answers
Player follow touch when holding down your finger on the screen (C#) 1 Answer
Multitouch doesn't work 1 Answer