Last active
January 13, 2018 15:16
-
-
Save blue0513/e2e5d2d36593119e9f884c3aac033315 to your computer and use it in GitHub Desktop.
WalkDetector for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use it with GyroManager | |
// https://gist.github.com/blue0513/36443a54a852ba997ebbe274065ab647 | |
using UnityEngine; | |
using System.Collections; | |
public class WalkDetector : MonoBehaviour { | |
private Vector3 acc; | |
private float[] walk; | |
private bool topFlag = false; | |
private bool downFlag = false; | |
private float timer = 0f; | |
private float vertical = 0f; | |
private Vector3 walkdistance = Vector3.zero; | |
private Vector3 firstAttitude = new Vector3(0f,0f,1f); | |
private Vector3 secondAttitude = new Vector3(0f,0f,1f); | |
// Custamizable private variables | |
private float dotthreshold = 0.995f; | |
private float stepGain = 1f; | |
private float delta = 1.5f; | |
private float offset = 1.0f; | |
private float threshold = 0.02f; | |
// Public variables | |
public int walkCounter = 0; | |
public GyroManager gyro; | |
public GameObject gyroObj; | |
// Use this for initialization | |
void Start () { | |
walk = new float[5]; | |
for(int i=0; i<5; i++){ | |
walk[i] = 0f; | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
timer += Time.deltaTime; | |
acc = Input.acceleration; | |
SlopeDetect(); | |
WalkCount(); | |
} | |
// Userの上下動を検知 | |
private void WalkCount(){ | |
// buffer | |
walk[4] = walk[3]; | |
walk[3] = walk[2]; | |
walk[2] = walk[1]; | |
walk[1] = walk[0]; | |
walk[0] = vertical; // 上下方向の加速度 | |
float top; | |
float down; | |
top = walk[2]; | |
down = walk[2]; | |
// 加速度が正であり,1frame前後の加速度よりも大きかったら,flag = true | |
if(top > walk[3]*delta && top > walk[1]*delta && Mathf.Abs(top) > threshold && Mathf.Abs(top) < 0.3f){ | |
topFlag = true; | |
firstAttitude = gyroObj.transform.forward; | |
} | |
// 加速度が負であり,1frame前後の加速度よりも小さかったら,flag = true | |
if(down < walk[1]*delta && down < walk[3]*delta && topFlag && Mathf.Abs(down) > threshold && Mathf.Abs(down) < 0.3f){ | |
downFlag = true; | |
secondAttitude = gyroObj.transform.forward; | |
} | |
// 1つの波形ごとに処理 | |
if(topFlag && downFlag){ | |
topFlag = false; | |
downFlag = false; | |
// 誤作動を防ぐため,見回し動作時には発火させない | |
double dot = Vector3.Dot(firstAttitude, secondAttitude); | |
if(dot > dotthreshold){ | |
walkdistance = DirectionDetect(stepGain); // 向きによった距離 | |
walkCounter++; // 歩数 | |
Debug.Log("walk"); | |
} | |
}else{ | |
walkdistance = Vector3.zero; | |
} | |
} | |
// 上向き方向のみの検出 | |
private void SlopeDetect(){ | |
float cos; | |
float sin; | |
// 下を向いているときは,反応させない | |
if(gyro.slopeX > 40f){ | |
vertical = 0f; | |
return; | |
} | |
cos = Mathf.Cos(gyro.slopeX*Mathf.Deg2Rad); | |
sin = Mathf.Sin(gyro.slopeX*Mathf.Deg2Rad); | |
vertical = acc.y*cos + acc.z*sin + offset; | |
} | |
// 横向き方向のみの検出 | |
private Vector3 DirectionDetect(float step){ | |
float cosZ; | |
float cosX; | |
cosZ = Mathf.Cos(gyro.slopeY*Mathf.Deg2Rad); | |
cosX = Mathf.Cos((90f-gyro.slopeY)*Mathf.Deg2Rad); | |
Vector3 vec = new Vector3(cosX, 0f, cosZ); | |
return (vec*step); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment