• 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 andrewwebber25 · Jul 28, 2017 at 03:06 PM · controllerplayer movementtouch controlsmultitouchtouch screen

Issue with Touch Buttons

Hey everyone, sorry for the generic question but I don't know how to word this without it sounding confusing. Basically I have a left and right arrow on my screen that move the player when physically held on the phone (touch controls). My problem is that when you click and hold the right arrow button, the player walks right (like they should) but will keep walking even if you drag your finger off of that button. I don't mean that once you release the button he keeps walking, cause he doesn't. I mean that if you press and hold the button and slide your finger to anywhere else on the screen (while never taking your finger off the screen) he keeps walking. How do I make it that if the player slides there finger off of the button, the function stops playing? Here is my code:

          if (Input.GetKey(KeyCode.LeftArrow))
          {
              rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
              transform.localScale = new Vector3(-1, 1, 1);
  
          }
         if (Input.GetKey(KeyCode.RightArrow))
          {
              rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
              transform.localScale = new Vector3(-1, 1, 1);
          }
  
   
          if (moveright)
          {
              rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
              transform.localScale = new Vector3(-1, 1, 1);
           }
          if (moveleft)
          {
              rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
              transform.localScale = new Vector3(-1, 1, 1);
          }
  
      }

If this is something that can be done in inspector view or if their is an alternative, let me know. Thanks guys :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nyro · Jul 28, 2017 at 03:34 PM

You say that tou are using touch controllers, but your code is using the arrow keys.

Seems like you are using the Unity UI, so just add an Event Trigger component an use it to update your functions.

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 andrewwebber25 · Jul 28, 2017 at 06:56 PM 0
Share

Here is what I have for my Event Triggers for my RightArrow They are coming from my Touch Script. There is RightArrow which when pressed will make the player walk right. Then there is ReleaseRightArrow which will stop the player from moving when the person takes there finger off the screen. I need to have it that when They move their finger off the arrow icon, it stops. [1]

Here is the little code I have for RightArrow in my Touch Script:

 public void RightArrow()
     {
         player.moveright = true;
         player.moveleft = false;
     }
 
     public void ReleaseRightArrow()
     {
         player.moveright = false;
     }

Any other code pertaining to the RightArrow can be found in my original post

[1]: /storage/temp/98646-capture.jpg

capture.jpg (29.0 kB)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

73 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

Related Questions

OnDrag methods not being called on Android in Unity 2018.3.5f1 2 Answers

Mobile Joystick doesn't work when Mouse Cursor is locked 0 Answers

Topdown player movement with controller problem 1 Answer

All Unity Engine games' controls not working 1 Answer

Help with my Dash move? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges