Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Drag by touch issue "lag"

Discussion in 'iOS and tvOS' started by Tapgames, Apr 18, 2012.

  1. Tapgames

    Tapgames

    Joined:
    Dec 1, 2009
    Posts:
    242
    Hi all,

    I use this code to move a object on the screen where my finger is moving.
    It is working but it's slow "lag".
    When I move my finger slowly on the screen its following but when you go just a little faster it stops following and I have to go back to the object to reselect it and start moving again.

    Is there a way to do this better? :)

    Roy

    Code (csharp):
    1.  
    2. function Update () {
    3.  
    4.     for (var touch : Touch in Input.touches){
    5.  
    6.         var ray = Camera.main.ScreenPointToRay(touch.position);
    7.  
    8.         var hit : RaycastHit;
    9.  
    10.         if (Physics.Raycast (ray, hit, 100)) {
    11.  
    12.             if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) {
    13.  
    14.                 var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0);
    15.  
    16.                 object.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z - 0.5));
    17.  
    18.             }
    19.  
    20.         }
    21.  
    22.     }
    23. }
    24.  
     
  2. Tapgames

    Tapgames

    Joined:
    Dec 1, 2009
    Posts:
    242
  3. J_P_

    J_P_

    Joined:
    Jan 9, 2010
    Posts:
    1,027
    As far as I know, it's a hardware/OS limitation.
     
  4. RolfBertram_dot_me

    RolfBertram_dot_me

    Joined:
    Mar 1, 2011
    Posts:
    128
    Try function FixedUpdate instead.
     
  5. oneir0naut0

    oneir0naut0

    Joined:
    Jul 11, 2012
    Posts:
    1
    I know this is old, but one thing I found was to include the touch . phase == stationary in your checking of which phases to do something. I have found not doing this causes the object to stutter and drop, most likely when the user's finger pauses.
     
  6. praleadanut

    praleadanut

    Joined:
    Feb 18, 2014
    Posts:
    2
    Unknown identifier : 'object'