Skip to content

Instantly share code, notes, and snippets.

@grimmdev
Created July 15, 2016 21:39
Show Gist options
  • Save grimmdev/64c73f58cb434408b862f459af06402f to your computer and use it in GitHub Desktop.
Save grimmdev/64c73f58cb434408b862f459af06402f to your computer and use it in GitHub Desktop.
VR Walk Controller, Walk in place to move forward.
using UnityEngine;
using System.Collections;
public class WalkVRController : MonoBehaviour {
[SerializeField]
private float walkSpeed = 2f;
private float walkNormalize = 0.1f;
[SerializeField]
private float sensorThreshold = 1f;
[SerializeField]
private Transform targetTransform;
private float walkCount = 0;
private void Awake() {
Input.gyro.enabled = true;
}
private void Update () {
if (Shake ()) {
walkCount += 1;
LogController.i.Log ("Walked Steps:" + walkCount);
targetTransform.Translate (0, 0, walkNormalize * walkSpeed);
}
}
private bool Shake()
{
float x = Input.gyro.userAcceleration.x;
float length = Mathf.Sqrt (x * x);
if (length >= sensorThreshold)
return true;
else
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment