• 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
1
Question by ajayrangn · May 03, 2017 at 07:38 AM · touchscreentouch controlsgettouch

Count a touch in half of screen

i need to count touches in left and ride side of android screen

this my code to control player

if (Input.touchCount > 0) {

         foreach (Touch touch in Input.touches) {

             if(touch.position.x > Screen .width /2)
             {                        
                 doattack();
             } 


             else if (touch.position.x > Screen.width/2 ) {
                 playerjump();
             }
         }            

     }



if i tap left side of screen the player does attacks again and again so, i want to do attack for single touch and another attack for another touch. (Only in left side)

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 antx · May 03, 2017 at 11:14 AM 0
Share

as Bilelmnasser wrote.

Also: your code is testing the same side of the screen twice. $$anonymous$$ake it just an else for the second part.

(and remove the blank between "Screen" and ".width" in the first test)

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bilelmnasser · May 03, 2017 at 08:48 AM

remove foreach and use only Input.touches[0]

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
avatar image
1

Answer by RecyclingBen · May 03, 2017 at 11:36 AM

try something like this

  foreach (Touch touch in Input.touches) {

  if((touch.position.x > Screen.width / 2) && (touch.phase == TouchPhase.Began)) {

  doattack();

  }
  
  if((touch.position.x > Screen.width / 2) && (touch.phase == TouchPhase.Began)) {

  playerjump();

  }
  }

this way you will only attack and jump if you touched the specific side of the screen and you've only just tapped the screen.

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 spencerz · May 16, 2019 at 06:29 PM 0
Share

When I do this method @RecyclingBen, if i tap one side and hold my finger down, it allows me to use my other finger on the other sides which is no buen for me.. How can I avoid that?

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

6 People are following this question.

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

Related Questions

Input.GetTouch(0) doesn't work 2 Answers

Movement along with touch of finger 2 Answers

Multitouch problem 0 Answers

grid-based block placement system with touch input 1 Answer

How to detect whether a particular touch has ended? 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