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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question Touch.DeltaPosition not working as expected in WebGL

Discussion in 'Scripting' started by EO-Altacc, Mar 31, 2023.

  1. EO-Altacc


    Feb 19, 2019
    I have the following script for touch-zooming. Basically Im getting thee difference in distance between the 2 touches between the last and current frames and then just using the value to increase or decrease the FOV.

    This works as expected when built onto a mobile device, but when running in WebGL on that same device, it doesn't work as expected. To begin, the axes are all backwards. So I added a +/- 1 integer (the variable "sign") to flip it in Web GL. This was a relatively easy fix.

    Code (CSharp):
    1. private void TouchZoom() //zooms in on the midpoint between two finger touch if a certain threshold is exceeded.
    2.     {
    3.         if (Input.touchCount != 2)
    4.             return;
    5.         Touch touch1 = Input.GetTouch(0);
    6.         Touch touch2 = Input.GetTouch(1);
    8.         if (touch1.phase == TouchPhase.Moved && touch2.phase == TouchPhase.Moved)
    9.         {
    10.             Vector2 t1Previous = touch1.position - touch1.deltaPosition;
    11.             Vector2 t2Previous = touch2.position - touch2.deltaPosition;
    12.             float oldTouchDistance = Vector2.Distance(t1Previous, t2Previous);
    13.             float currentTouchDistance = Vector2.Distance(touch1.position, touch2.position);
    14.             float deltaDistance = currentTouchDistance - oldTouchDistance;
    15.             Camera.main.fieldOfView -= sign * deltaDistance * AdjustForZoom() * touchZoomScaler * Time.deltaTime;
    16.             Camera.main.fieldOfView = Mathf.Clamp(Camera.main.fieldOfView, fovLimit[0], fovLimit[1]);          
    17.         }      
    18.     }
    The second problem is that Touch.deltaPosition doesn't seem to work the same way. In WebGL the first touch immediately snaps the FOV base on distance between the two fingers, as it treats the last frame's two mid points as having as a 0 distance. this the difference is just the distance between the current point. This does not happen on device.

    How would I fix this issue. Should I get the script to skip the first 1-2 frames or something? Is this a know issue in WebGL or some sort of Unity Bug?

    Thanks in advance. :)