- Home /
The Input.GetMouseButton not working properly on Android.
I have the following code of C# to control my player
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerControl : MonoBehaviour { public Rigidbody rb; public float forwardforce = 2000f; public float sidewaysforce = 500f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rb.AddForce(0, 0, forwardforce * Time.deltaTime); // add force iin Z-direction
if (Input.GetMouseButton(1))
{
rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0,ForceMode.VelocityChange);
}
if (Input.GetMouseButton(0))
{
rb.AddForce(-sidewaysforce * Time.deltaTime, 0, 0,ForceMode.VelocityChange);
}
if(rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
It is working 100% the way it should on Unity software by running the game. However, when I export it to android it doesn't work as it should be. It is only working for left mouse click key where ever I touch on screen. In simple words, my player need to move left and right on left side and right side touch on screen, respectively, but it is only moving left even if I click on right side of touch screen. I am a beginner in this gaming, the help matters a lot. Thank you.
Answer by farhanJi · Sep 08, 2019 at 09:25 AM
Thank you @ZeBarba for your suggestion. I applied the third solution and it is working fine.
Here is the corrected code if any body facing same problem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public Rigidbody rb;
public float forwardforce = 2000f;
public float sidewaysforce = 500f;
float screenWidth = Screen.width;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rb.AddForce(0, 0, forwardforce * Time.deltaTime); // add force iin Z-direction
Touch touch = Input.GetTouch(0);
if (touch.position.x > (screenWidth / 2))
{
rb.AddForce(sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (touch.position.x < (screenWidth / 2))
{
rb.AddForce(-sidewaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
Answer by ZeBarba · Sep 07, 2019 at 04:36 PM
You are using only GetMouseButton().
Unity maps the whole screen as the left mouse button (button 0). It does not divide the screen or something like that.
You need to treat the mobile input specifically using one of those 3 options:
1-) Create virtual button on your UI, using a Canvas. (not that nice, because you will have more stuff on your screen)
2-) Use the simulateMouseWithTouches option (Documentation: https://docs.unity3d.com/ScriptReference/Input-simulateMouseWithTouches.html and https://docs.unity3d.com/Manual/MobileInput.html)
3-) Create a script to detect which side of the screen is touched, and plug this logic into your code.
There are probably other ways, but I could only think of there 3.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Creating an app for mobile and browsers 1 Answer
Adapt OnMouseDown to control with Gamepad? 0 Answers
tags on ContactFilter2D? 0 Answers