I want to pass the direction of the mobile device from the deviceorientation function of the web page to Unity to do the control object x,y,z but I have not been able to successfully get the x,y,z axis that matches the physical phone Want to ask me where is the error?
private Quaternion _CurrentGyro;
private Quaternion _OriginGyro;
private Quaternion _OriginGyroInverse;
private Vector3 _Pointer;
public GameObject phoneObject;phoneObject
void Start()
{
_OriginGyro = phoneObject.transform.rotation;
_OriginGyroInverse = Quaternion.Inverse(_OriginGyro);
_Pointer = Vector3.forward;
}
void AngleChange(string X, string Y, string Z)
{
if (float.TryParse(X, out float parsedX) &&
float.TryParse(Y, out float parsedY) &&
float.TryParse(Z, out float parsedZ))
{
// Quaternion adjustedGyro = Quaternion.Euler(-parsedX, -parsedZ, parsedY);
Quaternion adjustedGyro = Quaternion.Euler(-parsedX, 0, 0);
adjustedGyro *= Quaternion.Euler(0, -parsedY, 0);
adjustedGyro *= Quaternion.Euler(0, 0, parsedZ);
_CurrentGyro = adjustedGyro;
_Pointer = _CurrentGyro * Vector3.forward;
phoneObject.transform.rotation = _OriginGyroInverse * _CurrentGyro;
Debug.Log("Device rotation angle: " + Quaternion.Angle(_CurrentGyro, _OriginGyro));
Debug.Log("The device is pointing: " + _OriginGyroInverse * _Pointer);
}
else
{
Debug.LogWarning("Unable to parse alpha, beta or gamma value。");
}
}
Thanks in advance!