Author Topic: Rotation Sluggish on mobile/tablet  (Read 2711 times)

Dakcenturi

  • Guest
Rotation Sluggish on mobile/tablet
« on: January 30, 2013, 08:42:05 PM »
So I have a script to handle rotating sprits in NGUI. If I run it in the UNity editor or even build a web deploy and run that it acts fine.

However, if I build it for iOS or Android there is always a delay when initially moving the wheel before it actually starts to rotate and causes the object to *jump* in it's rotation. I'm not sure if this is an issue with the code itself or an issue with the code hanging off of NGUI triggers or what.

Any thoughts would be greatly appreciated. This is the last piece holding things up for release and I've tried a couple different fixes but with no apparent luck.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SpinWithMouse2 : MonoBehaviour {
  5.         public Camera Cam;
  6.         private Vector3 currentLoc;
  7.         private float dotUp;
  8.         private float dotRight;
  9.  
  10.         private Vector3 Right;
  11.         private Vector3 Up;
  12.  
  13.         void Start ()
  14.         {
  15.         //Set initial up and right directions for dot product calc
  16.         Right = transform.right;
  17.         Up = transform.up;
  18.         }
  19.  
  20.         public void OnPress ()
  21.         {
  22.             Ray ray = Cam.ScreenPointToRay(UICamera.currentTouch.pos);
  23.             RaycastHit hit;
  24.              
  25.             if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8))
  26.                 {
  27.                         currentLoc = transform.InverseTransformPoint(hit.point);
  28.                 currentLoc.z = 0;
  29.                 currentLoc = transform.TransformPoint(currentLoc);
  30.            
  31.                 //initialize dot products
  32.                 dotUp = Vector3.Dot(Up.normalized, (currentLoc - transform.position).normalized);
  33.                 dotRight = Vector3.Dot(Right.normalized, (currentLoc - transform.position).normalized);
  34.             }
  35.         }
  36.  
  37.         public void OnDrag ()
  38.         {
  39.    
  40.         Ray ray = Cam.ScreenPointToRay(UICamera.currentTouch.pos);
  41.         RaycastHit hit;
  42.      
  43.         if (Physics.Raycast(ray, out hit, Mathf.Infinity, 1 << 8))
  44.                 {
  45.                 Vector3 newLoc = transform.InverseTransformPoint(hit.point);
  46.                 newLoc.z = 0;
  47.                 newLoc = transform.TransformPoint(newLoc);
  48.    
  49.                 //Calculate the dot product between initial state and current mouse location
  50.                 float newDotUp = Vector3.Dot(Up.normalized, (newLoc - transform.position).normalized);
  51.                 float newDotRight = Vector3.Dot(Right.normalized, (newLoc - transform.position).normalized);
  52.    
  53.                 //calculate angle between previous mouse location and current mouse location
  54.                 float ang = Vector3.Angle(currentLoc - transform.position, newLoc - transform.position);
  55.    
  56.                 //Determine which direction to rotate.  Based off of previous and current dot products.
  57.                 if (newDotUp >= 0)
  58.                         {
  59.                         if (dotUp >= 0)
  60.                                 {
  61.                                 if (newDotRight > dotRight)
  62.                                         {
  63.                                 transform.Rotate(0, 0, -ang);
  64.                                 } else
  65.                                 transform.Rotate(0, 0, ang);
  66.                         } else
  67.                                 {
  68.                                 if (newDotRight > dotRight)
  69.                                         {
  70.                                 transform.Rotate(0, 0, ang);
  71.                                 } else
  72.                                 transform.Rotate(0, 0, -ang);
  73.                         }
  74.                 } else
  75.                         {
  76.                         if (dotUp < 0)
  77.                                 {
  78.                                 if (newDotRight > dotRight)
  79.                                         {
  80.                                 transform.Rotate(0, 0, ang);
  81.                                 } else
  82.                                 transform.Rotate(0, 0, -ang);
  83.                         } else
  84.                                 {
  85.                                 if (newDotRight > dotRight)
  86.                                         {
  87.                                 transform.Rotate(0, 0, -ang);
  88.                                 } else
  89.                                 transform.Rotate(0, 0, ang);
  90.                         }
  91.                 }
  92.    
  93.                 //Reset previous information
  94.                 currentLoc = newLoc;
  95.                 dotUp = newDotUp;
  96.                 dotRight = newDotRight;
  97.         }
  98.         }
  99. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Rotation Sluggish on mobile/tablet
« Reply #1 on: January 31, 2013, 12:55:27 AM »
UICamera has a pair of thresholds -- one for mouse, another for touch. Touch one is set to 40 by default. Simply lower it.

Dakcenturi

  • Guest
Re: Rotation Sluggish on mobile/tablet
« Reply #2 on: January 31, 2013, 10:45:37 PM »
Exactly it. Out of curiosity why have the settings set so high for touch but so low for mouse input? I basically set the touch to the same as the mouse and it is working fine now.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Rotation Sluggish on mobile/tablet
« Reply #3 on: February 01, 2013, 12:48:02 AM »
Fat fingers. :)