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: http://answers.unity3d.com/questions/1097916/how-to-use-on-screen-buttons-in-unity-5-urgent.html#comment-1265675
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
Answer by aditya007 · Nov 03, 2016 at 04:16 AM
If the answer in the link doesn't work for you. Then you can try the following :
Make an GameObject and attach your Sprite on it (For it's appearance).
Attach a Collider on it acc. to it's shape, and mark it as "Is Trigger".
Attach a newly created script (name it whatever you like) to the GameObject.
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.
If this solved your problem, mark my answer as correct :)
Answer by Bwhang · Nov 04, 2016 at 04:49 AM
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'
If your problem is solved, then accept your answer and close this question :)
I didn't get a chance to try your method. I didn't want to accept my answer since it was kind of a cop out: did it in the traditional method.
I see. if you get time, try my method and see if that also works for you.
Your answer
Follow this Question
Related Questions
Block Input.GetMouseButtonDown() when clicking on UI element 2 Answers
Switching between mouse/keyboard and controller for UI navigation (using Input System) 0 Answers
How to differ between a mousedown over the map and a click on a GUI-Button 1 Answer
How do I let users change the background color of a selected word in an input field? 0 Answers
Porting to mobile how to assign input manager axes to GUIButtons. 0 Answers