LukeTrynchuk / BaseProject

0 stars 0 forks source link

Unity AR Adjust Direction To Match Direction Of Real World In Phone. #46

Closed LoopAllan closed 5 years ago

LoopAllan commented 5 years ago

I want to know how can I match direction to be same with real world direction in game.I had used Compass feature in phone unity provided.However, i found Input.Compass value did not available.I try to experiment it. First,create a project and create a script with code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class test : MonoBehaviour {
        string str = "";
        Compass compass;
    // Use this for initialization
        IEnumerator Start () {
            yield return new WaitUntil(()=>Input.GetKeyUp(KeyCode.Space));
            Debug.Log("start");
            Input.compass.enabled = true;
            compass = Input.compass;
    }

    void Update () {
            if (!Input.compass.enabled) return;
            str = string.Format("compass.headingAccuracy={0}\ncompass.magneticHeading= {1}\ncompass.rawVector={2}\ncompass.trueHeading={3}\ncompass.timestamp={4}",compass.headingAccuracy,compass.magneticHeading,compass.rawVector,compass.trueHeading,compass.timestamp );
    }

        private void OnGUI()
        {
             GUI.Label(new Rect(100, 100, 500, 100), str);
        }
}

then build it to device for test,i launch app 4 times with different direction then turn to the north.Result was always different, so i really don`t know what to do... 😢

LukeTrynchuk commented 5 years ago

Very interesting, I did not know that MonoBehaviour had a Start method that returned an IEnumerator.

As you mentioned, this is an app for a phone. I would try restructuring your Start Method. Rather than making the start method a coroutine, you could just make it return void and initialize your compass.

void Start() { Input.compass.enabled = true; compass = Input.compass; }

If I had to guess. The phone has no equivalent to the Space key from the keyboard.