问题
I'm using Unity to make a game that uses the Gyroscope. I am trying to detect the rotation of the device around the axis that goes through the screen, I'm assuming it's the Z-axis in Unity Space. I tried using Input.gyro.attitude.eulerAngles.z
but it doesn't stay constant when tilting the device around the other axes, I want it to stay constant so that only the "Z-axis" determines the rotation of a car's steering wheel, for example. How can I implement this?
EDIT:
I tried doing something else. I used Input.gyro.rotationRateUnbiased
instead of Input.gyro.attitude.eulerAngles
Here's the code:
Vector3 gInput = Input.gyro.rotationRateUnbiased;
gyroEuler += gInput * Time.deltaTime * Mathf.Rad2Deg;
float rollAngle = gyroEuler.z;
while (rollAngle < 0)
{
rollAngle += 360;
}
while (rollAngle > 360)
{
rollAngle -= 360;
}
But gyroEuler
will be set to (0, 0, 0)
regardless of the devices orientation at the start of the game so the device orientation will not be known in the beginning and also Input.gyro.rotationRateUnbiased
is very inaccurate. I want to rotate the screen the way many games, like Temple Run 2, do but I want to be able to rotate it 360 degrees and get that angle of rotation.
回答1:
Well I just tried it and it works well :
private void Start()
{
Input.gyro.enabled = true;
}
private void Update()
{
Vector3 previousEulerAngles = transform.eulerAngles;
Vector3 gyroInput = -Input.gyro.rotationRateUnbiased;
Vector3 targetEulerAngles = previousEulerAngles + gyroInput * Time.deltaTime * Mathf.Rad2Deg;
targetEulerAngles.x = 0.0f; // Only this line has been added
targetEulerAngles.z = 0.0f;
transform.eulerAngles = targetEulerAngles;
//You should also be able do it in one line if you want:
//transform.eulerAngles = new Vector3(0.0f, transform.eulerAngles.y - Input.gyro.rotationRateUnbiased.y * Time.deltaTime * Mathf.Rad2Deg, 0.0f);
}
What you need to do is create a central Transform (or use your player) and parent your Camera as child of it. Then set the Camera z-distance, height and angle to adjust your view. Finally attach the above script to the central Transform: rotating it will make your Camera move around following an arc.
EDIT: If you want to use Input.gyro.attitude
instead you can do it as follow
private Vector3 startEulerAngles;
private Vector3 startGyroAttitudeToEuler;
private void Start()
{
Input.gyro.enabled = true;
startEulerAngles = transform.eulerAngles;
startGyroAttitudeToEuler = Input.gyro.attitude.eulerAngles;
}
private void Update()
{
Vector3 deltaEulerAngles = Input.gyro.attitude.eulerAngles - startGyroAttitudeToEuler;
deltaEulerAngles.x = 0.0f;
deltaEulerAngles.z = 0.0f;
transform.eulerAngles = startEulerAngles - deltaEulerAngles;
}
public void ResetGyro()
{
startGyroAttitudeToEuler = Input.gyro.attitude.eulerAngles;
}
EDIT 2:
I guess what you are looking for will be something like this: you can then check upVec.y
and upVec.x
for your Down/Up and Left/Right axis.
First create an empty GameObject that will be used as a dummy containing gyroscope rotation and set it in phoneDummy
field of the script.
public Transform phoneDummy;
private void Start()
{
Input.gyro.enabled = true;
}
private void Update()
{
Vector3 gyroEuler = Input.gyro.attitude.eulerAngles;
phoneDummy.transform.eulerAngles = new Vector3(-1.0f * gyroEuler.x, -1.0f * gyroEuler.y, gyroEuler.z);
Vector3 upVec = phoneDummy.transform.InverseTransformDirection(-1f * Vector3.forward);
}
upVec.y
will be your pitch and upVec.x
will be your roll axis.
(check here for axis reference)
But keep in mind values from Input.gyro
are far from flawless: you may want to use Input.compass.magneticHeading
if you are looking for a more persistent value (but of course less precise one).
Hope this helps,
回答2:
Gyroscope jyro;
private void Start()
{
jyro = Input.gyro;
jyro.enabled = true;
}
void Update()
{
if (SystemInfo.supportsGyroscope)
{
transform.rotation = new Quaternion(0, 0, -Input.gyro.attitude.z, Input.gyro.attitude.w);
}
}
Took me ages, but you just need to use two of the quaternion values
来源:https://stackoverflow.com/questions/41927052/unity-gyroscope-rotation-around-one-axis-only