How to change PC keys into buttons for Android

Hey,
I have a script with shooting but the only problem I have is that its useless on Android because it needs mouse and keyboard and I want to change them to work for buttons but I don’t know how.

I want to change if (Input.GetKey(KeyCode.Mouse0) and if (Input.GetKeyDown(KeyCode.R) to buttons in image.
3918286--334174--Bez tytułu.png
Can someone help?

Here is code for shooting + reloading if needed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Shooting : MonoBehaviour
{

    public GameObject theBullet;
    public Transform barrelEnd;

    public int bulletSpeed;
    public float despawnTime = 3.0f;

    public bool shootAble = true;
    public float waitBeforeNextShot = 0.25f;

    public int curAmmo;
    public int fullAmmo = 30;
    public int backupAmmo;
    public float reloadTime = 3.0f;
    public float curReloadTime;
    public bool isReloading;

    public Text curAmmoText;
    public Text backupAmmoText;

    public void Start()
    {
        curAmmo = curAmmo;
        backupAmmo = backupAmmo;
    }

    private void Update()
    {
        curAmmoText.text = curAmmo.ToString();
        backupAmmoText.text = backupAmmo.ToString();

        if (Input.GetKey(KeyCode.Mouse0) && !isReloading && curAmmo > 0)
        {
            if (shootAble)
            {
                curAmmo--;
                shootAble = false;
                Shoot();
                StartCoroutine(ShootingYield());
            }
        }

        if (Input.GetKeyDown(KeyCode.R) && !isReloading && backupAmmo > 0)
        {
            curReloadTime = reloadTime;
            isReloading = true;
            Reload();
        }

        if (isReloading)
        {
            curReloadTime -= Time.fixedDeltaTime;

            if (curReloadTime <= 0)
            {
                isReloading = false;
            }
        }
    }

    void Reload()
    {
        var shot = fullAmmo - curAmmo;

        if (backupAmmo < shot)
        {
            curAmmo += backupAmmo;
            backupAmmo = 0;
        }
        else
        {
            curAmmo += shot;
            backupAmmo -= shot;
        }
    }

    IEnumerator ShootingYield()
    {
        yield return new WaitForSeconds(waitBeforeNextShot);
        shootAble = true;
    }

    void Shoot()
    {
        var bullet = Instantiate(theBullet, barrelEnd.position, barrelEnd.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * bulletSpeed;

        Destroy(bullet, despawnTime);
    }
}

You can use the Input.GetTouch method to get data about touch events in your code. You can add a script to your various on-screen components to get this data and perform behaviors appropriately, so actual implementation would include you adding a script to your UI game objects, implementing IPointerClickHandler on those scripts and using the position value from Input.GetTouch and doing some vector math with the UI game object’s attached rectTransform position to get the movement direction (Or just straight up setting a boolean on a appropriate component if no direction is needed). If you expose the references publicly you will be able to link the components in the inspector, allowing your scripts to communicate with one another. As it stands you might need to rewrite, or at least move, portions of your script, and you would need to change it again if you also wanted to support/revert to keyboard input. If you’re not pressed for time and want to learn, I’d recommend looking into something called the “Adapter” pattern.

Put simply, an adapter is an interface to a concrete implementation of something. It is a layer of abstraction between use and implementation. For example, if you were making a website, and you were using MySQL as a database, you wouldn’t connect and use the database’s code api directly, because when the time to inevitably change your database system, say to a NoSQL db or PostgreSQL, you would have to trawl through your code and replace every instance of the old api calls with the new ones - which is inefficient and a waste of time. Instead you would describe your interactions with the database through a new interface (which we call an adapter), and implement each set of interactions with each database system separately. This decreases coupling between code components and lets you plug in whatever implementation you want without having to change the rest of your code.

This idea can be extended to interactions with any system, including the unity Input system, your computer’s filesystem, or just about anything where there may be multiple implementations that can be switched in and out. Just don’t use Input directly in your scripts, only in the appropriate adapter.

Using adapters also has the benefit of making your code easy to write automated tests for, since creating a mock/stubbed implementation becomes trivial.

So basically, what you need to do is :

1 - Write an interface that describes your inputs in terms of behaviors
2 - Write two separate implementations of this interface, one for mouse/keyboard controls, and one for touchscreen devices
3 - Resolve which implementation you should use during runtime (You can also set up/activate any additional UI elements at this point, if needed, which will interact with this adapter (Your touch controls); keyboard input is easier since the .GetKey/.GetKeyDown stuff are all just static methods on Input and you can just check if the appropriate key is pressed in your adapter implementation, no interaction with other components)
4 - Use this resolved interface instead of calling Input.GetKey / Input.GetKeyDown / Input.* in your player/shooting script… example method names in your adapter interface might be stuff like WasReloadInput / GetMovementInput / WasShootingInput (Pretty weird, but I’m sure you can think of more appropriate ones)

There’s plenty of resources out there describing the adapter pattern, such as this stack overflow answer or this tutorialspoint tutorial.

Of course, you don’t need to write an adapter, but I can’t recommend looking into it at some point strongly enough. This may all be a bit much but I hope it helps :slight_smile: