Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Bwhang · Nov 02, 2016 at 05:33 PM · uiinputgui-button

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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 :

  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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aditya007 · Nov 03, 2016 at 05:22 AM 0
Share

If this solved your problem, mark my answer as correct :)

avatar image
0

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'

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aditya007 · Nov 07, 2016 at 04:26 AM 0
Share

If your problem is solved, then accept your answer and close this question :)

avatar image aditya007 · Nov 07, 2016 at 04:26 AM 0
Share

And, did you tried my method?

avatar image Bwhang aditya007 · Nov 07, 2016 at 05:25 AM 0
Share

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.

avatar image aditya007 Bwhang · Nov 07, 2016 at 11:32 AM 0
Share

I see. if you get time, try my method and see if that also works for you.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

117 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges