Pufferfiz

Unity Rotation Script

Sep 8th, 2013
232
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private Quaternion originalRotation;
  2. private float startAngle = 0;
  3.  
  4. public void Start()
  5. {  
  6.     originalRotation = this.transform.rotation;
  7. }
  8.  
  9. public void InputIsDown()
  10. {
  11.     #if UNITY_IPHONE || UNITY_ANDROID
  12.         originalRotation = this.transform.rotation;
  13.         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
  14.         Vector3 vector = Input.GetTouch(0).position - screenPos;
  15.         startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
  16.         //startAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg; // uncomment to pop to where mouse is
  17.     #else
  18.         originalRotation = this.transform.rotation;
  19.         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
  20.         Vector3 vector = Input.mousePosition - screenPos;
  21.         startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
  22.         //startAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg; // uncomment to pop to where mouse is
  23.  
  24.     #endif
  25. }
  26.  
  27. public void InputIsHeld()
  28. {
  29.     #if UNITY_IPHONE || UNITY_ANDROID
  30.         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
  31.         Vector3 vector = Input.GetTouch(0).position - screenPos;
  32.         float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
  33.         Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle , this.transform.forward);
  34.         newRotation.y = 0; //This and the line below, may need to be changed depending on what axis the object is rotating on.
  35.         newRotation.eulerAngles = new Vector3(0,0,newRotation.eulerAngles.z);
  36.         this.transform.rotation = originalRotation * newRotation;
  37.     #else
  38.         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
  39.         Vector3 vector = Input.mousePosition - screenPos;
  40.         float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
  41.         Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle , this.transform.forward);
  42.         newRotation.y = 0; //see comment from above
  43.         newRotation.eulerAngles = new Vector3(0,0,newRotation.eulerAngles.z);
  44.         this.transform.rotation = originalRotation * newRotation;
  45.      #endif
  46.  }
RAW Paste Data