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
4
Question by Josh 14 · Jul 15, 2011 at 02:13 AM · inputiphonescreentouches

Touching left or right side of iPhone screen?

I basically just want to know how I would go about detecting if the user is touching either the left or right side of the screen? I'm assuming I would use Input.touches, but I'm not sure where to go from there. Thanks in advance for any help!

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
12
Best Answer

Answer by Josh 14 · Jul 15, 2011 at 03:10 AM

After a little bit of research into the Touch class I've answered my own question. A simple script to detect touching the left or right side of the screen would be:

 if (Input.touchCount == 1)
 {
     var touch = Input.touches[0];
     if (touch.position.x < Screen.width/2)
     {
         DoLeftSideStuff();
     }
     else if (touch.position.x > Screen.width/2)
     {
         DoRightSideStuff();
     }
 }
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 Peter G · Jul 15, 2011 at 03:42 AM 5
Share

I'm going to give you a few suggestions to maybe make your code better:

  1. The more important one, you would be wise to switch Input.touches[0] with Input.GetTouch(0). The latter does not allocate any temporary variables and on iOS that is an important feature because garbage collection is expensive and the fewer variables you can allocate the less often you will need to.

  2. I would suggest using if(Input.touchCount > 0 ) ins$$anonymous$$d of ==1 because then it will work properly if the user puts two fingers down. You still only want the first touch, but you can still retrieve the first touch even if there are more.

avatar image
0

Answer by Valer407 · Feb 05, 2019 at 12:39 AM

When you don't want have multiply clicking when finger touch screen here is code:

  void Update () {
      if (Input.touchCount > 0)
      {
          var touch = Input.GetTouch(0);
          if (touch.position.x < Screen.width/2)
          {
             if(Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 Debug.Log("Left click");
             }
          }
          else if (touch.position.x > Screen.width/2)
          {
             if (Input.GetTouch(0).phase == TouchPhase.Began)
             {
                 Debug.Log("Right click");
             }
         }

      }
  }
Comment
Add comment · 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

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

Ray error with touches.position 1 Answer

Managing Multiple Touch Inputs 0 Answers

Exporting to Touch Screen Monitors or Touch Screen Displays 0 Answers

Mac menu bar interfering with click events 0 Answers

Unity iPhone - Changing to Landscape in code 4 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