Android Touch Input not working in Build
I've seen a question like this one multiple times but it never got answered to work for me. So, I've worked with Touch Input in the past but this is my first time working with Touch Input in Unity 2017. Below is my Code. This Code works when I play the Game and test it via Unity Remote, but not in the Build. Does anyone have a Idea? I tried so many things. :(
if (Input.GetMouseButtonDown(0))
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit.collider != null)
{
MapTile tile = hit.transform.GetComponent<MapTile>();
if (tile != null)
{
entityMovement.MoveOffset(tile.Position - Position);
ShowWalkableTiles();
gameManager.PlayerDidTurn();
return;
}
}
}
/*else*/
if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
Debug.Log("Touch");
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit.collider != null)
{
MapTile tile = hit.transform.GetComponent<MapTile>();
if (tile != null)
{
entityMovement.MoveOffset(tile.Position - Position);
ShowWalkableTiles();
gameManager.PlayerDidTurn();
return;
}
}
}
Answer by Der-Die-Das · Feb 27, 2018 at 11:28 AM
It finaly worked by just using Input.touches[0] instead of Input.GetTouch(0). No clue why though.
I just stumbled across this question.
Sorry but this answer makes no sense. The touches property of the Input class is just:
public static Touch[] touches
{
get
{
int count = touchCount;
Touch[] touches = new Touch[count];
for (int q = 0; q < count; ++q)
touches[q] = GetTouch(q);
return touches;
}
}
So you effectively are just calling GetTouch(0)
but in a more expensive way.
Yeah, I know it doesn't make any sense.. But it worked in the end. I'm sorry for anyone who has the same problem but this doesn't solve it.
hey, i think have the same problem that you had, but i didn't understand the solution... I created an Android app. When I build it to 32 bit it works fine but when I build it to 64 bit the touch does not work. I tried to change all kinds of functions but once I install the app on my phone the touch does not work.
Please help me, I've built a lot of apps and it never happened to me
this is my code:
void Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit;
if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity)) { Debug.Log(hit.transform.gameObject.name);
if (hit.transform.gameObject.name == this.name) {
if (OnBlockPressed != null) {
OnBlockPressed(this);
}
}
} } }
Answer by Luisoft · Feb 26, 2018 at 10:39 AM
Try using touches instead of mouse clicks on your Android device.
Touch firstTouch = Input.GetTouch(0);
if (firstTouch.phase == TouchPhase.Began)
{
// The touch started this is equal to a MouseDown
}
Keep in mind you should have a class to abstract your Input code from your logic.
I did pretty much the same thing, didn't I? I just used TouchPhase.Ended ins$$anonymous$$d.
But yeah. I will seperate Input from Logic. Thanks
Answer by guilhermern · Feb 26, 2018 at 11:58 AM
Mouse simulation with touches can be enabled/disabled with Input.simulateMouseWithTouches option. By default this option is enabled.
I tried enabling it. Didn't work. I may try to disable it. Thanks
Your answer
Follow this Question
Related Questions
I am having trouble building the project for android 0 Answers
Remove IPostGenerateGradleAndroidProject callback at build time 0 Answers
Gradle build: Could not compile build.gradle error 1 Answer
Android build error 1 Answer
Failed Running UnityLinker.exe and UnityLinker.exe did not run properly! 0 Answers