Skip to content

Instantly share code, notes, and snippets.

@mikelovesrobots
Last active August 29, 2015 13:57
Show Gist options
  • Save mikelovesrobots/9398317 to your computer and use it in GitHub Desktop.
Save mikelovesrobots/9398317 to your computer and use it in GitHub Desktop.
Adapter for Unity that uses the mouse for input if the current device (e.g., the editor) doesn't support acceleration. MIT license, so go nuts.
using UnityEngine;
using System.Collections;
public class AccelerometerAdapter : MonoBehaviour {
public const float TILT_SENSITIVITY = 1.5f;
public Vector3 Input {
get {
if (SystemInfo.supportsAccelerometer) {
return UnityEngine.Input.acceleration * TILT_SENSITIVITY;
} else {
var mousePosition = UnityEngine.Input.mousePosition;
var x = (mousePosition.x - Screen.width / 2) / Screen.width;
var y = (mousePosition.y - Screen.height / 2) / Screen.height;
return new Vector3(x, y, 0) * TILT_SENSITIVITY;
}
}
}
}
using UnityEngine;
using System.Collections;
public class InputFilter : MonoBehaviour {
public float Filter = 5.0f;
public float Sensitivity = 4.0f;
public float XClamp = 3f;
public Vector3 Input;
private Vector3 accel;
public AccelerometerAdapter AccelerometerAdapter;
void Start() {
accel = AccelerometerAdapter.Input;
}
void Update() {
// filter the jerky acceleration in the variable accel:
accel = Vector3.Lerp(accel, AccelerometerAdapter.Input, Filter * Time.deltaTime);
var dir = accel * Sensitivity;
dir.x = Mathf.Clamp(dir.x, -XClamp, XClamp);
Input = dir;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment