- Home /
Converting mouse movement and input to android
I have a 2d top down shooter and i am trying to port this to android using the CrossPlatformInput with the onscreen thumbsticks but not sure how! i have the left stick working and the player moving! i want to duplicate the left stick and use it to shoot projectiles in each direction! at the moment my code is set up to use a mouse position and then fires a projectile with Input.GetMouseButton(0), how can i use the duplicate thumbstick to shoot projectiles in the direction i move it instead of using the mouse? here is my current code for the mosue and projectiles.
void Update ()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
transform.rotation = rotation;
if (Input.GetMouseButton(0))
{
if(Time.time >= shotTime)
{
Instantiate(projectile, shotPoint.position, transform.rotation);
cameraAnim.SetTrigger("shake");
shotTime = Time.time + timeBetweenShots;
}
}
}
Answer by Matt1000 · Dec 11, 2018 at 03:44 PM
If you want the equivalent of Input.GetMouseButton(0) on Android/Mobile you should do something like this:
void Update() {
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch();
//Do whatever you want with the touch.
}
}
On the other hand, if you dont want every touch and those in a particular zone only, I'd recommend using UI. Simply put a invisible button or panel in that zone and redirect the OnMouseDown to you gameObject.
I have thought of panel method but Im ai$$anonymous$$g to have a second UI thumbstick that shoots in all directions like a twin stick shooter!