Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class AccelerometerInputBehaviour : MonoBehaviour {
- [HideInInspector]
- public float output; //Float output
- public enum AxisType { x,y,z } //Accelered axis
- public AxisType axis;
- public float deadZone;
- public float maxAngle = 45;
- void Update () {
- float angle = 0;
- //Select axis
- switch (axis) {
- case AxisType.x:
- angle = Input.acceleration.x;
- break;
- case AxisType.y:
- angle = Input.acceleration.y;
- break;
- case AxisType.z:
- angle = Input.acceleration.z;
- break;
- }
- angle = angle * 90;
- //Clamp maximum
- angle = Mathf.Clamp(angle, -maxAngle, maxAngle);
- output = angle / 90 * (90 / maxAngle);
- //Apply deadZone
- var absoluteOutput = Mathf.Abs( output );
- if ( absoluteOutput < deadZone )
- output = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement