Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by rollinsals · Feb 24, 2015 at 06:29 AM · 2duimovement

Use UI buttons for movement

So I have a basic 2d movement script that works with Input.GetAxis just fine. Now I'm trying to change the input to an on-screen d-pad for mobile. The problem is it is only running the movement once and then won't run the function again.

      public void goRight(BaseEventData eventData) {
                     inputX = 1;
                     buttonheld = true;
                     Debug.Log ("You should be going right");
                 }
                 
                 public void release(BaseEventData eventData){
                     buttonheld = false;
                     inputX = 0;
                     inputY = 0;
                 }
 
 void Update ( )
         {
             
             if (buttonheld) {
                 
                                 if (inputX != 0 || inputY != 0) {
                                         
                                         Move (inputX, inputY);
                                 }
                         
                         }



This is the gist of what I'm doing. Any insights would be great.

Comment
Add comment · Show 1
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 Qasem2014 · Feb 24, 2015 at 07:03 AM 0
Share

where u assign the release and release function ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Feb 24, 2015 at 09:16 AM

OK, not got access to Unity so this is just from my head. Place your four buttons/Images on screen and create a script that has eight public functions in it.

 public void UpButtonOn()
 {
     upButton = true;
 }
 
 public void UpButtonOff()
 {
     upButton = false;
 }
 
 // repeat for left, right and down

Then in update just check those bool values and if true do the movement for that direction and handle events like left and right being pressed at the same time cancelling each other out. Call it DpadScript.cs

Drag that script onto the canvas directly.

On each button/image add an EventTrigger Component and add events to trigger on PointerEnter and PointerExit so for the up button add PointerEnter Event and drag the canvas (with the DpadScript on it) onto the slot that appears then from the dropdown select DpadScript -> UpButtonOn.

Add a PointerExit event and repeat but put it to DpadScript -> UpButtonOff.

Repeat that for each button adding a PointerEnter and PointerExit to the correct function to set the bool.

Should work but can't test as I don't have access to Unity at the moment.

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 rollinsals · Mar 06, 2015 at 04:48 AM 0
Share

Well that's essentially what I have but for whatever reason, it will run the move function once each then won't run again. So the buttons themselves work fine, but its a matter of connecting that event with the movement.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Have 2D Sprite Follow the Mouse in Straight Line 2 Answers

Unity 2D 4.6b UI: Why are instantiated prefab sprites rendering over UI elements? 1 Answer

How to Convert each 2D Array to GameObject 2 Answers

Moving object disappears then comes back 1 Answer

Top down 2d character controller sometimes walking backwards 1 Answer


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