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 /
avatar image
0
Question by andrewwebber25 · Jul 28, 2017 at 07:17 PM · player movementtouch controlstouchscreenevent triggeringunityui

Make touch buttons stop working after finger slides off.

Hey everyone, sorry for the weird title 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 for the Right Arrow.

Player Script:

         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);
         }


Touch Script:

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

Here is what Unity and Even Triggers look like. Sorry my Right Arrow image is hard to see, I have the Opacity down but it is located on the bottom right of the Scene / Game View.

Thanks everyone! :)

capture.jpg (101.7 kB)
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
1
Best Answer

Answer by · Jul 28, 2017 at 08:25 PM

Use OnPointerExit.

https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.OnPointerExit.html

Comment
Add comment · Show 3 · 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 08:42 PM 0
Share

Okay cool, how exactly would I use it. would i put it in my Touch Script like:

public void OnPointerExit(EventSystem.PointerEventData eventData);

Because that says EventSystem namespace could not be found. I even added "using UnityEngine.EventSystems;" to the top of my Script thinking it would identify it. Or would i put "RightArrow" in there somewhere?

avatar image andrewwebber25 · Jul 28, 2017 at 09:00 PM 1
Share

Inside Unity in the Event Trigger component click on "Add New Event Type", choose Pointer Exit, then make the same thing as you did with Pointer Up

avatar image andrewwebber25 · Jul 31, 2017 at 12:59 PM 0
Share

You got it! Sorry for the noob question, I really appreciate the help!

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

68 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

Related Questions

How to make basic touch screen controls 1 Answer

Touch controls for Pong based video games for Android 0 Answers

Problem in multi touch 0 Answers

move object x axis with finger touch 0 Answers

Player can jump on non ground items 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