Search Unity

Touch stays active after touch has been released

Discussion in 'Scripting' started by FrozenCake, Aug 2, 2019.

  1. FrozenCake

    FrozenCake

    Joined:
    Sep 5, 2015
    Posts:
    4
    Hello, currently I am working on a 2D Sidescroller for Android with Input.GetTouch as a way to read the Input. But every time I stop touching the area which is used to move the Character, he continues to move in that direction even though I'm not touching anything.

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         int Touches = Input.touchCount;
    4.  
    5.         if(Touches > 0)
    6.         {
    7.             for (int i = 0; i < Touches; i++)
    8.             {
    9.                 Touch touch = Input.GetTouch(i);
    10.  
    11.                 Movement(touch);
    12.                 Jump(touch);
    13.                 Crouch(touch);
    14.             }
    15.         }
    16.  
    17.         // If there is no input then stop moving
    18.         if (LeftTouches <= 0)
    19.         {
    20.             MovementDirection = 0;
    21.         }
    22.     }
    23.  
    24.     void Movement(Touch touch)
    25.     {
    26.         // Count the Touches
    27.         LeftTouches = 0;
    28.  
    29.         if (IsMovingLeft(touch))
    30.         {
    31.             MovementDirection = -1;
    32.             LeftTouches++;
    33.         }
    34.         if (IsMovingRight(touch))
    35.         {
    36.             MovementDirection = 1;
    37.             LeftTouches++;
    38.         }
    39.  
    40.         if (!IsCrouching(touch) && !WasCrouchJumping)
    41.         {
    42.             RB.velocity = new Vector2(MovementDirection * MovementSpeed, RB.velocity.y);
    43.         }
    44.     }
    45.  
    46.     public bool IsMovingRight(Touch touch)
    47.     {
    48.         if (touch.position.x > Screen.width / 4 && touch.position.x < Screen.width / 2)
    49.         {
    50.             return true;
    51.         }
    52.         else
    53.         {
    54.             return false;
    55.         }
    56.     }
    57.  
    58.     public bool IsMovingLeft(Touch touch)
    59.     {
    60.         if (touch.position.x < Screen.width / 4)
    61.         {
    62.             return true;
    63.         }
    64.         else
    65.         {
    66.             return false;
    67.         }
    68.     }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    You are resetting LeftTouches in the Movement function wich is called only if there are active touch(es). Thus, when player releases stops touching, the value remains > 0.
     
  3. FrozenCake

    FrozenCake

    Joined:
    Sep 5, 2015
    Posts:
    4
    That was a mistake but it's still not fixed. I think it has something to do with the way Unity handles Touch Inputs. It emulates a mouse cursor and that cursor is still clicking that button even if you stop touching it.
     
  4. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    No, this is not the case. You can disable mouse emulation and test again. If you issue isn't fixed, then it means your scripts have more erorrs. Did you posted all the code related?
     
  5. FrozenCake

    FrozenCake

    Joined:
    Sep 5, 2015
    Posts:
    4
    I tried turning off mouse simulation but that didn't work. And that should be all the necessaire code but just in case here is the rest of it.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MobilePlayerController : MonoBehaviour
    6. {
    7.     [Header("Movement")]
    8.     public float MovementSpeed = 10;
    9.  
    10.     [Header("Jump")]
    11.     public float JumpForce = 10;
    12.     public float JumpDelta = 1;
    13.     public int JumpTimes = 1;
    14.  
    15.     [Header("IsGrounded")]
    16.     public float IsGroundOffSet;
    17.     public Vector2 IsGroundBoxSize;
    18.  
    19.     [Header("Slide")]
    20.     public float SlideSize = 0.5f;
    21.     public float SlideDelta;
    22.     public float SlideSpeed;
    23.     public float SlideJumpMultiplyer;
    24.  
    25.     [Header("Components")]
    26.     public Rigidbody2D RB;
    27.     public Collider2D BC;
    28.  
    29.     // Private
    30.     //[Header("Later Private")]
    31.     bool WasCrouchJumping = false;
    32.     bool Jumped;
    33.     int TimesJumped;
    34.     float MovementDirection;
    35.     Vector2 StartPos;
    36.     Vector2 Direction;
    37.     bool DirectionChosen;
    38.     bool IsCrouched;
    39.     int LeftTouches;
    40.  
    41.     private void Start()
    42.     {
    43.         Input.simulateMouseWithTouches = false;
    44.     }
    45.  
    46.     void Update()
    47.     {
    48.         // Count the Touches
    49.         LeftTouches = 0;
    50.  
    51.         int Touches = Input.touchCount;
    52.  
    53.         if(Touches > 0)
    54.         {
    55.             for (int i = 0; i < Touches; i++)
    56.             {
    57.                 Touch touch = Input.GetTouch(i);
    58.  
    59.                 Movement(touch);
    60.                 Jump(touch);
    61.                 Crouch(touch);
    62.             }
    63.         }
    64.  
    65.         // If there is no input then stop moving
    66.         if (LeftTouches <= 0)
    67.         {
    68.             MovementDirection = 0;
    69.         }
    70.     }
    71.  
    72.     void Movement(Touch touch)
    73.     {
    74.         if (IsMovingLeft(touch))
    75.         {
    76.             MovementDirection = -1;
    77.             LeftTouches++;
    78.         }
    79.         if (IsMovingRight(touch))
    80.         {
    81.             MovementDirection = 1;
    82.             LeftTouches++;
    83.         }
    84.  
    85.         if (!IsCrouching(touch) && !WasCrouchJumping)
    86.         {
    87.             RB.velocity = new Vector2(MovementDirection * MovementSpeed, RB.velocity.y);
    88.         }
    89.     }
    90.  
    91.     public bool IsMovingRight(Touch touch)
    92.     {
    93.         if (touch.position.x > Screen.width / 4 && touch.position.x < Screen.width / 2)
    94.         {
    95.             return true;
    96.         }
    97.         else
    98.         {
    99.             return false;
    100.         }
    101.     }
    102.  
    103.     public bool IsMovingLeft(Touch touch)
    104.     {
    105.         if (touch.position.x < Screen.width / 4)
    106.         {
    107.             return true;
    108.         }
    109.         else
    110.         {
    111.             return false;
    112.         }
    113.     }
    114.  
    115.     public void Jump(Touch touch)
    116.     {
    117.         if (IsGrounded())
    118.         {
    119.             if (Jumped)
    120.             {
    121.                 WasCrouchJumping = false;
    122.                 Jumped = false;
    123.                 TimesJumped = 0;
    124.             }
    125.  
    126.             if (IsJumping(touch))
    127.             {
    128.                 TimesJumped++;
    129.                 if (IsCrouched)
    130.                 {
    131.                     RB.AddForce(this.transform.up * JumpForce * SlideJumpMultiplyer, ForceMode2D.Impulse);
    132.                     WasCrouchJumping = true;
    133.                     this.transform.localScale = new Vector3(1, Mathf.Lerp(this.transform.localScale.x, 1, SlideDelta), 1);
    134.                     IsCrouched = false;
    135.                 }
    136.                 else
    137.                 {
    138.                     RB.AddForce(this.transform.up * JumpForce, ForceMode2D.Impulse);
    139.                 }
    140.             }
    141.         }
    142.  
    143.         if (TimesJumped < JumpTimes && !IsGrounded())
    144.         {
    145.             if (IsJumping(touch))
    146.             {
    147.                 TimesJumped++;
    148.                 RB.AddForce(this.transform.up * JumpForce, ForceMode2D.Impulse);
    149.             }
    150.         }
    151.  
    152.         if (!IsGrounded())
    153.         {
    154.             Jumped = true;
    155.         }
    156.     }
    157.  
    158.     public void Crouch(Touch touch)
    159.     {
    160.         if (IsCrouching(touch))
    161.         {
    162.             if (IsGrounded())
    163.             {
    164.                 RB.velocity = new Vector2(RB.velocity.x * SlideSpeed, RB.velocity.y);
    165.             }
    166.             this.transform.localScale = new Vector3(1, Mathf.Lerp(this.transform.localScale.x, SlideSize, SlideDelta), 1);
    167.             IsCrouched = true;
    168.         }
    169.  
    170.         if (IsCrouching(touch) && touch.phase == TouchPhase.Ended)
    171.         {
    172.             this.transform.localScale = new Vector3(1, Mathf.Lerp(this.transform.localScale.x, 1, SlideDelta), 1);
    173.             IsCrouched = false;
    174.         }
    175.     }
    176.  
    177.     public bool IsJumping(Touch touch)
    178.     {
    179.         if((touch.position.x > Screen.width / 2 && touch.position.y > Screen.height / 2))
    180.         {
    181.             return true;
    182.         }
    183.         else
    184.         {
    185.             return false;
    186.         }
    187.     }
    188.  
    189.     public bool IsCrouching(Touch touch)
    190.     {
    191.         if ((touch.position.x > Screen.width / 2 && touch.position.y < Screen.height / 2))
    192.         {
    193.             return true;
    194.         }
    195.         else
    196.         {
    197.             return false;
    198.         }
    199.     }
    200.  
    201.  
    202.     public bool IsGrounded()
    203.     {
    204.         return Physics2D.OverlapBox(new Vector2(this.transform.position.x, this.transform.position.y - (BC.bounds.size.y/2 + IsGroundBoxSize.y + IsGroundOffSet)), IsGroundBoxSize, 0f);
    205.     }
    206. }
    207.  
    208.  
    209.