Virtual keySystem

I have using unity for a week now. and when I want to export to android I forgot that I have to use a virtual key .-.
how could you make a virtual key system in unity?

u just add a sprite into scene, and give an invisible map for the trigger.
or can I just click on the sprite without using an invisible map?

and how u make invisible mapping? .-.
so when you touch left side the player walks left, and vice versa.

thanks for any help :smile:

You could technically use an invisible mapping on the screen and listen for touch events on these invisible objects (which must at least have a collider on them to receive clicks or touches). I think this method is over complicated though.

An alternative would be to listen for touch events on each side of the screen and act accordingly to each event. Hereโ€™s some sample code to get you started:

using UnityEngine;
using System.Collections;

public class TouchMovement : MonoBehaviour {

    private float w;
    private float h;

	void Start () {
        // Screen width and height
	    w = Screen.width;
        h = Screen.height;
	}
	
	void Update () {
        int i = 0;

        // Loop over every touch found
        while (i < Input.touchCount)
        {
            // Is this the beginning phase of the touch?
            if (Input.GetTouch(i).phase == TouchPhase.Began)
            {
                // Does the touch happens on the right side of the screen?
                if (Input.GetTouch(i).position.x > w / 2) {
                    // Move your character right
                    
                }

                // Does the touch happens on the left side of the screen?
                if (Input.GetTouch(i).position.x < w / 2){
                    // Move your character left
                    
                }
            }
            ++i;
        }
	}
}

Let me know if you have any questions! :slight_smile: