Author Topic: Sharing Spin with Mouse Script  (Read 3703 times)

Lord_Meshadieme

  • Guest
Sharing Spin with Mouse Script
« on: May 02, 2012, 11:58:54 PM »
Hi peeps,

i'd like to share my modified version of "spin with mouse" script that comes with nGUI.

basically it now allows you to select which axis to spin on as well as the Delta direction X/Y (True/False) of the mouse.

  1. using UnityEngine;
  2.  
  3. [AddComponentMenu("NGUI/Examples/Spin With Mouse")]
  4. public class SpinWithMouse : MonoBehaviour
  5. {
  6.         public enum Axis {
  7.                 XAxis = 0,
  8.                 YAxis = 1,
  9.                 ZAxis = 2,
  10.         }
  11.         public Transform target;
  12.         public float speed = 1f;
  13.         public bool direction = true;
  14.         public Axis whichAxis = Axis.YAxis;
  15.  
  16.         Transform mTrans;
  17.  
  18.         void Start ()
  19.         {
  20.                 mTrans = transform;
  21.         }
  22.  
  23.         void OnDrag (Vector2 delta)
  24.         {
  25.                 UICamera.currentTouch.clickNotification = UICamera.ClickNotification.None;
  26.                 float deltaval = delta.x;
  27.                 Quaternion newRotation = Quaternion.Euler(0f, -0.5f * deltaval * speed, 0f);
  28.                 if (direction) {
  29.                         deltaval = delta.x;
  30.                 } else {
  31.                         deltaval = delta.y;
  32.                 }
  33.                 switch (whichAxis) {
  34.                 case Axis.XAxis:
  35.                         newRotation = Quaternion.Euler( -0.5f * deltaval * speed, 0f, 0f);
  36.                         break;
  37.                 case Axis.YAxis:
  38.                         newRotation = Quaternion.Euler(0f, -0.5f * deltaval * speed, 0f);
  39.                         break;
  40.                 case Axis.ZAxis:
  41.                         newRotation = Quaternion.Euler(0f, 0f,  -0.5f * deltaval * speed);
  42.                         break;
  43.                 }
  44.                 if (target != null) {
  45.                         target.localRotation = newRotation * target.localRotation;
  46.                 } else {
  47.                         mTrans.localRotation = newRotation * mTrans.localRotation;
  48.                 }
  49.         }
  50. }

I know its not a huge change but i thought it might help someone.

Regards,
Lord Meshadieme
« Last Edit: May 03, 2012, 03:08:31 AM by Lord_Meshadieme »