Advertisement
Muk99

AccelerometerInputBehaviour

Jan 8th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class AccelerometerInputBehaviour : MonoBehaviour {
  6.  
  7.     [HideInInspector]
  8.     public float output;                        //Float output
  9.  
  10.     public enum AxisType { x,y,z }              //Accelered axis
  11.  
  12.     public AxisType axis;                  
  13.     public float deadZone;
  14.     public float maxAngle = 45;
  15.  
  16.     void Update () {
  17.         float angle = 0;
  18.  
  19.         //Select axis
  20.         switch (axis) {
  21.         case AxisType.x:
  22.             angle = Input.acceleration.x;
  23.             break;
  24.         case AxisType.y:
  25.             angle = Input.acceleration.y;
  26.             break;
  27.         case AxisType.z:
  28.             angle = Input.acceleration.z;
  29.             break;
  30.         }
  31.  
  32.         angle = angle * 90;
  33.  
  34.         //Clamp maximum
  35.         angle = Mathf.Clamp(angle, -maxAngle, maxAngle);
  36.  
  37.         output = angle / 90 * (90 / maxAngle);
  38.  
  39.         //Apply deadZone
  40.         var absoluteOutput = Mathf.Abs( output );
  41.         if ( absoluteOutput < deadZone )
  42.             output = 0;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement