Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Touch phase is moved when touch pressure changed

Discussion in 'General Discussion' started by aqibjamshed98, Nov 14, 2021.

  1. aqibjamshed98

    aqibjamshed98

    Joined:
    Nov 14, 2021
    Posts:
    1
    I am trying to scale my object by a finger touch just like in Jelly Shift game. It should scale up/down when I move my finger up and down and it should stop when the touch is in stationary phase.

    The problem is when I pressure the screen without moving my finger, touch phase changes to "moved" and my object scales down. The harder I pressure, the faster it scales down. I tried to clamp touch.pressure to 1 every time it detects a finger on screen, but it didn't work. How can I ignore pressure of the touch and scale the object just by finger moves ? Thank you so much.

    Here I check the input by touch anysoll

    Code (CSharp):
    1. private void CheckInput()
    2.             {
    3.                 if (Input.touchCount == 1)
    4.                 {
    5.                     _touch = Input.GetTouch(0);
    6.                     // I've tried to clamp touch pressure like below but it didn't work
    7.                     _touch.pressure = 1;
    8.                     if (_touch.phase == TouchPhase.Began)
    9.                     {
    10.                         _isInputAllowed = true;
    11.                     }
    12.                     else if (_touch.phase == TouchPhase.Moved)
    13.                     {
    14.                         _isInputAllowed = true;
    15.                     }
    16.                     else if (_touch.phase == TouchPhase.Stationary)
    17.                     {
    18.                         _isInputAllowed = false;
    19.                         Vertical = 0f;
    20.                     }
    21.                     else if (_touch.phase == TouchPhase.Ended || _touch.phase == TouchPhase.Canceled)
    22.                     {
    23.                         _isInputAllowed = false;
    24.                         Vertical = 0f;
    25.                     }
    26.                     CalculateVerticalInput();
    27.                 }
    28.             }
    I process the input below

    Code (CSharp):
    1. private void CalculateVerticalInput()
    2.     {
    3.         if (_isInputAllowed)
    4.         {
    5.             if (_touch.deltaPosition.y > 0.5f)
    6.             {
    7.                 Vertical = 1f;
    8.             }
    9.             else if (_touch.deltaPosition.y < 0.5f)
    10.             {
    11.                 Vertical = -1f;
    12.             }
    13.             else
    14.             {
    15.                 Vertical = 0f;
    16.             }
    17.         }
    18.     }

    Object scaling method

    Code (CSharp):
    1. private void Scale()
    2.     {
    3.         if (_inputData.Vertical == 0)
    4.         {
    5.             return;
    6.         }
    7.         sourceScaleVal = (_inputData.Vertical > 0) ? _settings.MinScaleValue : _settings.MaxScaleValue;
    8.         targetScaleVal = (_inputData.Vertical > 0) ? _settings.MaxScaleValue : _settings.MinScaleValue;
    9.         transform.localScale = Vector3.MoveTowards(transform.localScale, new Vector3(sourceScaleVal, targetScaleVal, transform.localScale.z), _settings.ScalingSpeed * Time.deltaTime);
    10.         PlayerHelper.SetCurrentScale(transform.localScale);
    11.     }
     
    Last edited: Apr 5, 2022