我正在尝试开发一款可以检测汽车加速,制动和转弯速度的应用。我需要检测设备加速。加速度是高还是低?喜欢Google Play上的flo app。
我使用陀螺仪,我需要从陀螺仪中获得用户加速度(x,y,z轴)的最高值。 x,y和z的值每帧都在变化。我需要达到这个3轴的最高值以供以后使用。
现在我的当前代码看起来像这样
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
public Text x, y, z;
void Start()
{
Input.gyro.enabled = true;
}
void Update()
{
x.text = Mathf.Abs(Input.gyro.userAcceleration.x).ToString();
y.text = Mathf.Abs(Input.gyro.userAcceleration.y).ToString();
z.text = Mathf.Abs(Input.gyro.userAcceleration.z).ToString();
}
}
感谢您的帮助
答案 0 :(得分:0)
使用Time.deltaTime;
执行计时器,然后将Input.gyro.userAcceleration
存储到临时Vector3
变量中,然后比较以及来自Gyro的新值下一帧。如果新值为>
旧的vector3
值,则覆盖旧值。当计时器启动时,您可以使用旧值作为最高值。
void Start()
{
Input.gyro.enabled = true;
}
bool firstRun = true;
float counter = 0;
float calcTime = 1; //Find highest gyro value every 1 seconds
Vector3 oldGyroValue = Vector3.zero;
Vector3 highestGyroValue = Vector3.zero;
void Update()
{
gyroFrame();
}
void gyroFrame()
{
if (firstRun)
{
firstRun = false; //Set to false so that this if statement wont run again
oldGyroValue = Input.gyro.userAcceleration;
counter += Time.deltaTime; //Increment the timer to reach calcTime
return; //Exit if first run because there is no old value to compare with the new gyro value
}
//Check if we have not reached the calcTime seconds
if (counter <= calcTime)
{
Vector3 tempVector = Input.gyro.userAcceleration;
//Check if we have to Update X, Y, Z
if (tempVector.x > oldGyroValue.x)
{
oldGyroValue.x = tempVector.x;
}
if (tempVector.y > oldGyroValue.y)
{
oldGyroValue.y = tempVector.y;
}
if (tempVector.z > oldGyroValue.z)
{
oldGyroValue.z = tempVector.z;
}
counter += Time.deltaTime;
}
//We have reached the end of timer. Reset counter then get the highest value
else
{
counter = 0;
highestGyroValue = oldGyroValue;
Debug.Log("The Highest Values for X: " + highestGyroValue.x + " Y: " + highestGyroValue.y +
" Z: " + highestGyroValue.z);
}
}