- Home /
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)
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)
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.
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
 
 
             Follow this Question
Related Questions
Input.GetTouch(0) doesn't work 2 Answers
[SOLVED] Touch Controls Not Responsive 1 Answer
Make a Triangle instead of a "Rect" 1 Answer
Player moving opposite direction when there is a new touch. 2 Answers
Swipe Detection Best Practices 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                