Can an onscreen button be connected to an input?

I’m a newbie using javascript and unity 5.4.1. I’ve been working on a ball game. I now want to get the left and right movement on onscreen buttons for an android version and pc’s that have touch control.

I’ve looked up many answers in the forum regarding this but most deal with non-ball games where the left and right is easier. With a ball game, my movement is initially based on rotation that is then applied to the ball. Thus I’ve been looking into making my movement code into a function that can be assigned a Left and Right. Haven’t quite got it to work yet. I have a few things that complicate my controls: gravity swap and mario jump in certain situations but not all.

I came across one forum answer which was helpful:

However, this solution for me has the ball move when the onscreen button is released. The ball doesn’t move while the onscreen button is pressed. Another problem is that this solution created a different ball move method in order to work with the onscreen buttons.

I’ve spent a lot of time on my movement code so I don’t want to use a different method for touch controls.

Then it occurred to me, is there a Unity method to connect a Canvas’ onscreen button to my game’s Inputs? I’ve already got “Horizontal”, “Left”, and “Right” inputs setup. If this can be done, I can use the same movement code.

In short, I’d like a left Onscreen button press to trigger a “Left” press according to my inputs (same as if I was pressing Left Arrow or A on my keyboard or Left Analog stick or Left directional pad on my Xbox controller).

One additional question: should I not use the Button script method and instead use Event Trigger Pointer Down and Pointer Up for onscreen move left and right.

TIA

If the answer in the link doesn’t work for you. Then you can try the following :

  1. Make an GameObject and attach your Sprite on it (For it’s appearance).
  2. Attach a Collider on it acc. to it’s shape, and mark it as “Is Trigger”.
  3. Attach a newly created script (name it whatever you like) to the GameObject.
  4. Open the script in the Script editor and Define a function

void OnMouseDown(){ //This fucntion will be called when the user has pressed over this GameObject}

Then you can write your code inside that OnMouseDown().

For more Info, Check Unity Docs for OnMouseDown function.

I ended up addressing this the traditional way: created functions that could be attached to onscreen buttons. Used Event Trigger Pointer Down and Pointer Up.

Had problems with the pointer down only triggering once. Fixed it by adding the following above my movement code which is in the Update function:

if (guibuttondownL == true) {
    		Dir = -1; //Not sure why I have to set it here.  For some reason my Dir variable gets zeroed out everytime the Update function restarts if I set it below in my new functions.
    		
    	}
    
    	if (guibuttondownR == true) {
    		Dir = 1;
    		
    	}
    
    	if (guibuttondownL == false && guibuttondownR == false) { //This is for keyboard/joystick control
    		Dir = Input.GetAxis ("Horizontal");
    		
    	}

Beneath the Update function, I created the following functions to connect to the onscreen buttons:

 #if UNITY_ANDROID


 function onpointDownL(Dir : int){
 	guibuttondownL = true;
 	//Dir = -1;  // Doesn't work if I set Dir here.  Always gets zeroed out when Update starts again
 	
 }

 function onpointUpL(Dir : int){
 	guibuttondownL = false;
 	//Dir = 0;

 }

  function onpointDownR(Dir : int){
 	guibuttondownR = true;
 	//Dir = 1;

 }

 function onpointUpR(Dir : int){
 	guibuttondownR = false;
 	//Dir = 0;

 }


#endif

Lastly I set the following variables at the top of my script:

//Needed to make sure a gui button press runs multiple times
var guibuttondownL = false;
var guibuttondownR = false;
//var Dir : float; //Use this for PC version
var Dir : int; //Use this for Android version.  

Current difference between Android and PC versions is that the keyboard and joysticks pass in float values (keyboard does it due to the sensitivity setting). With the Android version, I’m just passing in a 1 or -1 thus it’s an ‘int’ rather than a ‘float’