Home > Blockchain >  Unity gyroscope read only 2 axis
Unity gyroscope read only 2 axis

Time:10-28

Hope you are having a great day, everyone! Thank you for stopping by!

I want to make it so that gyroscope will only change X and Y axis of an object. There is a code for one axis and for three axis (mostly used), but I couldn't find anything about two axis and wasn't able to do it myself after spending a night to make it work.

Thanks in advance!

CodePudding user response:

Reset Z-Axis or whatever axis before applying-

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;

    // Z-axis reseted, so it won't be applied. 
    deltaEulerAngles.z = 0.0f;

    transform.eulerAngles = startEulerAngles - deltaEulerAngles;
}
  • Related