Esempio n. 1
0
    // Update is called once per frame
    void Update()
    {
        //We create a new Translation and Rotation from the transform of the GameObject
        //The GameObject Translation and Rotation is updated by the pose driver
        var arTranslation = new Translation {
            Value = transform.position
        };
        var arRotation = new Rotation {
            Value = transform.rotation
        };
        //Now we update our ARPoseComponent with the updated Pose Driver data
        var arPose = new ARPoseComponent {
            translation = arTranslation,
            rotation    = arRotation
        };

        m_ClientSimGroup.SetSingleton <ARPoseComponent>(arPose);

        //If the player was spawned, we will move the AR camera to behind the spawn location
        if (!m_SpawnPositionQuery.IsEmptyIgnoreFilter)
        {
            //We grab the component from the Singleton
            var spawnPosition = m_ClientSimGroup.GetSingleton <SpawnPositionForARComponent>();
            // Debug.Log("spawn position is: " + spawnPosition.spawnTranslation.ToString());

            //We set the new pose to behind the player (0, 2, -10) (this is the same value the player is put in front of the pose in InputResponseMovementSystem)
            var newPoseTranslation = (spawnPosition.spawnTranslation) + (math.mul(spawnPosition.spawnRotation, new float3(0, 2, 0)).xyz) - (math.mul(spawnPosition.spawnRotation, new float3(0, 0, 10)).xyz);
            //The rotation will be the same
            var newPoseRotation = (spawnPosition.spawnRotation);

            // Debug.Log("calculated camera position is: " + newPoseTranslation.ToString());

            //MakeContentAppearAt requires a transform even though it is never used so we create a dummy transform
            Transform dummyTransform = new GameObject().transform;
            //First we will undo our last MakeContentAppearAt to go back to "normal"
            m_ARSessionOrigin.MakeContentAppearAt(dummyTransform, -1f * m_LastTranslation, Quaternion.Inverse(m_LastRotation));

            //Now we will update our LastTranslation and LastRotations to the values we are about to use
            //Because of how MakeContentAppearAt works we must do the inverse to move our camera where we want it
            m_LastTranslation = -1f * newPoseTranslation;
            m_LastRotation    = Quaternion.Inverse(newPoseRotation);
            //Now that we have set the variables we will use them to adjust the AR pose
            m_ARSessionOrigin.MakeContentAppearAt(dummyTransform, m_LastTranslation, m_LastRotation);

            // Debug.Log("transform after MakeContentAppearAt: " + transform.position.ToString());
            //Now we delete the entity so this only runs during an initial spawn
            m_ClientWorld.EntityManager.DestroyEntity(m_ClientSimGroup.GetSingletonEntity <SpawnPositionForARComponent>());
        }
    }
    //This function will navigate us to NavigationScene and connected with the clients/server about leaving
    void ClickedQuitGame()
    {
        //As a client if we were able to create an NCE we must add a request disconnect
        if (!m_ClientNetworkIdComponentQuery.IsEmptyIgnoreFilter)
        {
            var clientNCE = m_ClientSimulationSystemGroup.GetSingletonEntity <NetworkIdComponent>();
            m_ClientWorld.EntityManager.AddComponentData(clientNCE, new NetworkStreamRequestDisconnect());
        }

        //As a server if we were able to create an NCE we must add a request disconnect to all NCEs
        //We must to see if this was a host build
        if (m_ServerWorld != null)
        {
            //First we grab the array of NCEs
            var nceArray = m_ServerNetworkIdComponentQuery.ToEntityArray(Allocator.TempJob);
            for (int i = 0; i < nceArray.Length; i++)
            {
                //Then we add our NetworkStreamDisconnect component to tell the clients we are leaving
                m_ServerWorld.EntityManager.AddComponentData(nceArray[i], new NetworkStreamRequestDisconnect());
            }
            //Then we dispose of our array
            nceArray.Dispose();
        }

#if UNITY_EDITOR
        if (Application.isPlaying)
#endif
        SceneManager.LoadSceneAsync("NavigationScene");
#if UNITY_EDITOR
        else
        {
            Debug.Log("Loading: " + "NavigationScene");
        }
#endif
        if (ClientServerInfo.IsServer)
        {
            m_ServerWorld.GetExistingSystem <GhostDistancePartitioningSystem>().Enabled = false;
        }
    }