Home > Net >  how to build gyroscope above the current phone direction
how to build gyroscope above the current phone direction

Time:07-26

public class GyroControl : MonoBehaviour
{
    private bool gyroEnabled;
    private Gyroscope gyro;

    private GameObject cameraContainer;
    private Quaternion rot;


    private void Start()
    {
        cameraContainer = new GameObject("Camera Container");
        cameraContainer.transform.position = transform.position;
        transform.SetParent(cameraContainer.transform);


        gyroEnabled = EnableGyro();
    }

    private bool EnableGyro()
    {
        if (SystemInfo.supportsGyroscope)
        {
            gyro = Input.gyro;
            gyro.enabled = true;


            cameraContainer.transform.rotation = Quaternion.Euler(90f, 0f, -90f);
            rot = new Quaternion(0, 0, 1, 0);

            return true;
        }

        return false;
    }

    private void Update()
    {
        if (gyroEnabled)
        {
            transform.localRotation = gyro.attitude * rot;

        }
    }
}

i have a game that build with using gyroscope but the gyroscope use world rotation i don't want that i want whenever i start the game and my phone direction anywhere it makes it the start rotation , i mean if i start the game i will use the same camera rotation and move right and left by the gyro scope value .

i tried this :

    private Quaternion initialRot;

/

private bool EnableGyro()
{
    if (SystemInfo.supportsGyroscope)
    {
        gyro = Input.gyro;
        gyro.enabled = true;

        initialRot = gyro.attitude;

        cameraContainer.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        
        return true;
    }
    return false;
}

/

private void Update()
{
    if (gyroEnabled)
    {
        transform.localRotation = gyro.attitude * Quaternion.Inverse(initialRot);
    }
}

in the end i want to make the gyro value build above the current phone direciotn

CodePudding user response:

Your second script is the most logical solution. Try using vectors instead of Quaternions, as they should be easier to manage and debug. The initialRot variable, transform it into Vector3 adds in the Update () method:

 private Vector3 initialRot;
...

private bool EnableGyro()
{
    if (SystemInfo.supportsGyroscope)
    {
        gyro = Input.gyro;
        gyro.enabled = true;

        initialRot = gyro.attitude.eulerAngles;

        cameraContainer.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        
        return true;
    }
    return false;
}
...

private void Update()
{
    if (gyroEnabled)
    {
        transform.localEulerAngles = gyro.attitude.eulerAngles   initialRot;
    }
}

CodePudding user response:

You're almost correct. You need to do left multiplication with the inverse instead of right.

init:
    initialRot = getDeviceRotation()

step:
    localRot = inverse(initialRot)*getDeviceRotation()

If you only want to initialize the rotation about the gravity axis, you can implement and use the align function at https://stackoverflow.com/a/73103883/3159048

init:
    initialRot = align(getDeviceRotation(), gravityVec, gravityVec)
  • Related