Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Android Touch Issue

Discussion in 'Scripting' started by Nipmip, Jul 17, 2018.

  1. Nipmip

    Nipmip

    Joined:
    Jul 17, 2018
    Posts:
    5
    I have been googling this for a while, time to just ask. Basically I have this.

    Code (CSharp):
    1. Void FixedUpdate(){
    2.     Foreach (Touch touch in Input.touch){
    3.     if(touch.position.x > Screen.width/2 && !(touch.position.x < Screen.width/2)){
    4.     //do this
    5. }
    6.  
    7. if(!(touch.position.x > Screen.width/2) && touch.position.x < Screen.width/2){
    8.     //do this
    9. }
    10.  
    11. if(touch.position.x > Screen.width/2 && touch.position.x < Screen.width/2){
    12.     //do this
    13. }
    14.  
    15. }
    16. }
    Touching the left side of the screen works fine. Touching the right side of the screen works fine.

    The problem is the 3rd option. When I touch both sides of the screen together I want a 3rs action to take place and I'm not getting it. Any idea what I'm doing wrong?
     
  2. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    That won't work.
    You would need to do something like this:
    Code (CSharp):
    1.  
    2. bool leftTouching = false, rightTouching = false;
    3. float halfWidth = Screen.width * 0.5f;
    4.  
    5. foreach (Touch touch in Input.touches) {
    6.     if (touch.position.x > halfWidth && !(touch.position.x < halfWidth)) {
    7.         rightTouching = true;
    8.     }
    9.    
    10.     if (!(touch.position.x > halfWidth) && touch.position.x < halfWidth) {
    11.         leftTouching = true;
    12.     }
    13. }
    14.  
    15. if (leftTouching && rightTouching) {
    16.     // Both sides being touched...
    17. }
    (Not tested)
     
    Nipmip likes this.
  3. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Parallel post, so do what F14M...(for gods sake :-D ) said
    In Addition: You were checking the same touch for being on left and right side of the screen.
     
  4. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Above answers are correct, also:

    Code (CSharp):
    1. if(touch.position.x > Screen.width/2 && !(touch.position.x < Screen.width/2)){
    2.     //do this
    3. }
    why the && !touch part? If x is larger than half the screen width it can't also be smaller.
     
  5. Nipmip

    Nipmip

    Joined:
    Jul 17, 2018
    Posts:
    5
    Thanks Yall.. Soon as I get off work I will make this happen. I appreciate the responses
     
  6. mjzx

    mjzx

    Joined:
    Aug 18, 2013
    Posts:
    114
    Yes you're absolutely right. Thanks for pointing that out.


    Hahaha, sorry about my name, I was much more immature back when I chose it 5 years ago ;)
     
  7. Nipmip

    Nipmip

    Joined:
    Jul 17, 2018
    Posts:
    5
    Just not happening for me.

    To answer why I did the (&& !touch) .. In the event I was touching both sides of the screen I wanted all single actions to be halted.

    Basically, I want these touch results..

    Left screen only = rotate object counter clockwise

    Right screen only = rotate clockwise

    Left and right screen = no rotate, move forward.

    It's odd. When I press left screen, I get the desired result. When I press right I get the desired result. When I press both, I don't get the (left && right) result, I instead get the output from just left and just right at the same time.

    Thanks for the replys. I will keep working at it. If I figure out why none of the above seem to make a difference, I will post it
     
  8. Nipmip

    Nipmip

    Joined:
    Jul 17, 2018
    Posts:
    5
    Ok... It took some doing, (damn beginners lol) … This may be completely the wrong way to go about it, but it works exactly as I needed. if anyone finds this thread and wants to know what worked for me, here is the code I ended up with. I wish I could remember where I found what lead me to getting it working so I could link straight to it. In any event here is what I ended up with. Again, thanks for your help. It is very appreciated!
    Code (CSharp):
    1.     //Handle Left And Right Screen Touches
    2.         if (Input.touchCount == 1)
    3.         {
    4.             Touch touchZero = Input.GetTouch(0);
    5.             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    6.  
    7.             if (touchZeroPrevPos.x > halfScreen) {
    8.                 //Do Right Event
    9.             }
    10.             if (touchZeroPrevPos.x < halfScreen) {
    11.                 //Do Left Event
    12.             }
    13.         }
    14.  
    15.     //Handle Combination Left And Right Screen Touch
    16.         if (Input.touchCount == 2)
    17.         {
    18.             Touch touchZero = Input.GetTouch(0);
    19.             Touch touchOne = Input.GetTouch(1);
    20.  
    21.             Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
    22.             Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
    23.  
    24.             if ((touchZeroPrevPos.x > halfScreen && touchOnePrevPos.x < halfScreen) || (touchZeroPrevPos.x < halfScreen && touchOnePrevPos.x > halfScreen)) {
    25.                 //Do Combination Event
    26.             }
    27.         }
     
  9. nilsdr

    nilsdr

    Joined:
    Oct 24, 2017
    Posts:
    374
    Good to hear you got it working, although the above post by Flamethrower should work aswel. Not sure why you're subtracting the deltaposition either? you're not detecting a drag or anything are you?