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

Android touch different than mouse

Discussion in 'Scripting' started by Alundra, Apr 22, 2014.

  1. Alundra

    Alundra

    Joined:
    Feb 25, 2014
    Posts:
    14
    Hi all,
    Question is quite simple about this code :
    Code (csharp):
    1.  
    2. if(Input.GetMouseButton(0)  !GameSettings.Instance.WaitAction)
    3. {
    4.   float AxisX = Input.GetAxis("Mouse X");
    5.   float AxisY = Input.GetAxis("Mouse Y");
    6.   float DragX = AxisX * DragSpeed;
    7.   float DragY = AxisY * DragSpeed;
    8.   LerpTargetPos -= new Vector3(DragX, DragY, 0.0f);
    9. }
    10.  
    This code works well on Desktop but on android that give very fast move.
    I tried this code :
    Code (csharp):
    1.  
    2. if((Input.touchCount > 0)  (Input.GetTouch(0).phase == TouchPhase.Moved)  !GameSettings.Instance.WaitAction)
    3. {
    4.   Touch Touch0 = Input.GetTouch(0);
    5.   float DeltaTime = (Touch0.deltaTime > 0.0f) ? (Time.deltaTime / Touch0.deltaTime) : 0.0f;
    6.   float AxisX = Touch0.deltaPosition.x * DeltaTime;
    7.   float AxisY = Touch0.deltaPosition.y * DeltaTime;
    8.   float DragX = AxisX * DragSpeed;
    9.   float DragY = AxisY * DragSpeed;
    10.   LerpTargetPos -= new Vector3(DragX, DragY, 0.0f);
    11. }
    12.  
    But that give fast move too.
    Do you have idea how fix this problem ?
    Thanks for the help
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    The first example, GetAxis() always returns a number between -1 and 1.

    Meanwhile, Touch.deltaPosition.y will be between -1 and 1 only if the user moves a single pixel in the frame. If they drag across an entire 1080p screen in a frame, you'd get a deltaPosition.y of 1080, which is humongous compared to 1.

    EDIT: How did you even use GetAxis() on Android? Are you using a gamepad instead of the touch screen?
     
    ezerai likes this.
  3. Alundra

    Alundra

    Joined:
    Feb 25, 2014
    Posts:
    14
    Just build using the same code as desktop and works but give fast move so it's not good.
    So, to have same result as desktop deltaPosition has to be divided by screen size ?
     
  4. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Maybe, but honestly I think it's more complicated than that. Touch is not meant to emulate a mouse. You need to think about what you want the touch input on mobile to achieve, and then use the Unity APIs that help you do that.
     
  5. Alundra

    Alundra

    Joined:
    Feb 25, 2014
    Posts:
    14
    My goal is pretty simple, on desktop it's straighforward.
    It's just to have a move on x-axis/y-axis based on the delta position based on a DragSpeed.
    In other words, do this desktop code working for android to have same result (or similar) :
    Code (csharp):
    1.  
    2. float AxisX = Input.GetAxis("Mouse X");
    3. float AxisY = Input.GetAxis("Mouse Y");
    4. float DragX = AxisX * DragSpeed;
    5. float DragY = AxisY * DragSpeed;
    6.  
     
    Last edited: Apr 23, 2014
  6. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Yeah, you said that already. What I meant was, how should the user interact with the tablet? Given that tablets have radically different screen resolutions is the number of pixels moved relevant? Probably not, which is why you asked about dividing by the screen width. All I am trying to say is a straightforward port of mouse code to touch code might get you a result, but it's not necessarily the best approach. The GetAxis function returns between +/- one. Dividing the touch movement by the screen size will mean the user having to move their finger the full width of the screen. That just seems unrealistic to me.
     
  7. Alundra

    Alundra

    Joined:
    Feb 25, 2014
    Posts:
    14
    Here the new code :
    Code (csharp):
    1.  
    2. // Check if the user is dragging and the input is allowed.
    3. if(Input.GetMouseButton(0)  !GameSettings.Instance.WaitAction)
    4. {
    5.   if(IsDragging == false)
    6.   {
    7.     float OldDragPositionX = Input.mousePosition.x / Screen.width;
    8.     float OldDragPositionY = Input.mousePosition.y / Screen.height;
    9.     OldDragPosition = new Vector2(OldDragPositionX, OldDragPositionY);
    10.     IsDragging = true;
    11.   }
    12.   float NewDragPositionX = Input.mousePosition.x / Screen.width;
    13.   float NewDragPositionY = Input.mousePosition.y / Screen.height;
    14.   Vector2 NewDragPosition = new Vector2(NewDragPositionX, NewDragPositionY);
    15.   Vector2 DeltaDragPosition = NewDragPosition - OldDragPosition;
    16.   OldDragPosition = NewDragPosition;
    17.   Vector2 DeltaDragWithSpeed = DeltaDragPosition * DragSpeed * Time.deltaTime;
    18.   LerpTargetPos -= new Vector3(DeltaDragWithSpeed.x, DeltaDragWithSpeed.y, 0.0f);
    19.   LerpTargetPos = FixedCameraPosBasedOnParams(LerpTargetPos);
    20. }
    21. else
    22. {
    23.   IsDragging = false;
    24. }
    25.  
    It calcule the delta of mouse position in normalized way like that it's independent to screen size and mul by speed.
    Using #ifdef surely with GetTouch(0) the position can be calculated using the same way and give the same result.
    Is it a correct way ?
     
    Last edited: Apr 23, 2014