C# Unity3D-如何从Input.gyro获得最大加速度值
我正在尝试开发一款能够检测汽车加速、制动和转弯速度的应用程序。我需要检测设备加速度。加速度是高还是低?。比如谷歌Play上的flo应用程序 我使用的是陀螺仪,我需要从陀螺仪中获得用户加速度的最高值(x,y,z轴)。x、y和z的值每帧都在更改。我需要实现这3轴的最高值,以便以后使用 现在,我当前的代码如下所示C# Unity3D-如何从Input.gyro获得最大加速度值,c#,unity3d,accelerometer,gyroscope,C#,Unity3d,Accelerometer,Gyroscope,我正在尝试开发一款能够检测汽车加速、制动和转弯速度的应用程序。我需要检测设备加速度。加速度是高还是低?。比如谷歌Play上的flo应用程序 我使用的是陀螺仪,我需要从陀螺仪中获得用户加速度的最高值(x,y,z轴)。x、y和z的值每帧都在更改。我需要实现这3轴的最高值,以便以后使用 现在,我当前的代码如下所示 using UnityEngine; using System.Collections; using UnityEngine.UI; public class NewBehaviourSc
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();
}
}
感谢您的帮助使用
Time.deltaTime
执行计时器,然后将Input.gyro.userAcceleration
存储到临时Vector3
变量中,并稍后将其与下一帧中陀螺仪的新值进行比较。如果新值是
旧的矢量3
值,覆盖旧值。定时器启动时,可以使用旧值作为最高值
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);
}
}
void Start()
{
Input.gyro.enabled=真;
}
bool firstRun=true;
浮点计数器=0;
浮点数=1//每1秒找到最高陀螺仪值
Vector3 oldGyroValue=Vector3.0;
Vector3最高陀螺值=Vector3.0;
无效更新()
{
回转框架();
}
空框()
{
如果(首次运行)
{
firstRun=false;//设置为false,这样该if语句就不会再次运行
oldGyroValue=输入.gyro.userAcceleration;
计数器+=Time.deltaTime;//增加计时器以达到计算时间
return;//如果第一次运行,则退出,因为没有旧值可与新陀螺值进行比较
}
//检查我们是否未达到计算时间秒
if(计数器值.x)
{
oldGyroValue.x=tempVector.x;
}
if(tempVector.y>oldGyroValue.y)
{
oldGyroValue.y=tempVector.y;
}
if(tempVector.z>oldGyroValue.z)
{
oldGyroValue.z=tempVector.z;
}
计数器+=时间增量时间;
}
//我们已经到了计时器的末尾。重置计数器,然后获得最高值
其他的
{
计数器=0;
Highest GyroValue=旧GyroValue;
Log(“X:+highestGyroValue.X+”Y:+highestGyroValue.Y的最高值+
“Z:+最高值.Z);
}
}
您的问题令人困惑。。。。要检查哪个轴大于其中的3个轴?更多地解释你想做什么…不。我想得到每个轴的最大值,得到x,y,z的最大值,这没有意义。如果您确实需要帮助,请修改您的问题,并添加您所指的最高值以及您试图归档的内容。x、y和z的值在每一帧中都会发生变化。我需要获得这3个轴的最高值,以便以后使用OK。我会回来的。请修改您的问题以反映这一点。