Exemplo n.º 1
0
 // Update is called once per frame
 void Update()
 {
     currPhase = manager.WhichPhase();
     if (currPhase == GameplayManager.GamePhase.P1Hiding || currPhase == GameplayManager.GamePhase.P1Hiding)
     {
         renderer.enabled = true;
     }
     else
     {
         renderer.enabled = false;
     }
 }
 private void LateUpdate()
 {
     GameplayManager.GamePhase currPhase = manager.WhichPhase();
     if (currPhase == GameplayManager.GamePhase.P1Hiding || currPhase == GameplayManager.GamePhase.P2Hiding)
     {
         int childrenCount = gameObject.transform.childCount;
         for (int i = 0; i < childrenCount; i++)
         {
             Debug.Log(gameObject.transform.GetChild(i).name);
             gameObject.transform.GetChild(i).gameObject.GetComponent <Renderer>().enabled = true;
         }
     }
     else
     {
         int childrenCount = gameObject.transform.childCount;
         for (int i = 0; i < childrenCount; i++)
         {
             gameObject.transform.GetChild(i).gameObject.GetComponent <Renderer>().enabled = false;
         }
     }
 }
        /// <summary>
        /// The Unity Update() method.
        /// </summary>
        public void Update()
        {
            // Exit the app when the 'back' button is pressed.
            if (Input.GetKey(KeyCode.Escape))
            {
                //Commented out for now, don't want people exiting app in middle of game.
                //Application.Quit();
            }

            _QuitOnConnectionErrors();

            // Check that motion tracking is tracking.
            if (Session.Status != SessionStatus.Tracking)
            {
                const int lostTrackingSleepTimeout = 15;
                Screen.sleepTimeout = lostTrackingSleepTimeout;
                if (!m_IsQuitting && Session.Status.IsValid())
                {
                    SearchingForPlaneUI.SetActive(true);
                }

                return;
            }

            Screen.sleepTimeout = SleepTimeout.NeverSleep;

            // Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
            Session.GetTrackables <TrackedPlane>(m_NewPlanes, TrackableQueryFilter.New);
            for (int i = 0; i < m_NewPlanes.Count; i++)
            {
                // Instantiate a plane visualization prefab and set it to track the new plane. The transform is set to
                // the origin with an identity rotation since the mesh for our prefab is updated in Unity World
                // coordinates.
                GameObject planeObject = Instantiate(TrackedPlanePrefab, Vector3.zero, Quaternion.identity,
                                                     transform);
                planeObject.GetComponent <TrackedPlaneVisualizer>().Initialize(m_NewPlanes[i]);
            }

            // Hide snackbar when currently tracking at least one plane.
            Session.GetTrackables <TrackedPlane>(m_AllPlanes);
            bool showSearchingUI = true;

            for (int i = 0; i < m_AllPlanes.Count; i++)
            {
                if (m_AllPlanes[i].TrackingState == TrackingState.Tracking)
                {
                    showSearchingUI = false;
                    break;
                }
            }

            SearchingForPlaneUI.SetActive(showSearchingUI);

            /* if (currPhase == GameplayManager.GamePhase.Player1Hide || currPhase == GameplayManager.GamePhase.Player2Hide)
             * {
             *   List<Trackable> prevTrackables  = new List<Trackable>();
             *   Session.GetTrackables<Trackable>(prevTrackables, TrackableQueryFilter.New);
             *   foreach (Trackable t in prevTrackables){
             *       List<Anchor> anchors = new List<Anchor>();
             *       t.GetAllAnchors(anchors);
             *       foreach (Anchor anchor in anchors)
             *       {
             *           //Ghost is found
             *           int anchorChildren = anchor.transform.childCount;
             *           for (int i = 0; i < anchorChildren; i++)
             *           {
             *               GameObject child = anchor.transform.GetChild(i).gameObject;
             *               if (child.name.Contains("Ghost"))
             *               {
             *                   manager.removeGhost(child);
             *                   break;
             *               }
             *           }
             *           Destroy(anchor);
             *       }
             *   }
             * }*/

            // If the player has not touched the screen, we are done with this update.
            Touch touch;

            if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
            {
                return;
            }

            // Raycast against the location the player touched to search for planes.
            TrackableHit      hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                                              TrackableHitFlags.FeaturePointWithSurfaceNormal;

            GameplayManager.GamePhase currPhase = manager.WhichPhase();
            //Might need to get rid of this if but we'll test it
            if ((currPhase == GameplayManager.GamePhase.P1Hiding || currPhase == GameplayManager.GamePhase.P2Hiding) && manager.isPlacing())
            {
                if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
                {
                    Debug.Log("hit");

                    Debug.Log(currPhase);
                    if (manager.getAmtGhosts() < StaticVariables.Ghosts)
                    {
                        var ghostObject = Instantiate(AndyAndroidPrefab, hit.Pose.position, hit.Pose.rotation);
                        manager.poofFunc(hit.Pose.position, hit.Pose.rotation);

                        // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
                        // world evolves.
                        var anchor = hit.Trackable.CreateAnchor(hit.Pose);

                        // Andy should look at the camera but still be flush with the plane.
                        if ((hit.Flags & TrackableHitFlags.PlaneWithinPolygon) != TrackableHitFlags.None)
                        {
                            // Get the camera position and match the y-component with the hit position.
                            Vector3 cameraPositionSameY = FirstPersonCamera.transform.position;
                            cameraPositionSameY.y = hit.Pose.position.y;

                            // Have Andy look toward the camera respecting his "up" perspective, which may be from ceiling.
                            ghostObject.transform.LookAt(cameraPositionSameY, ghostObject.transform.up);
                        }

                        // Make Andy model a child of the anchor.
                        //  ghostObject.transform = new Vector3();
                        ghostObject.name = "Ghost" + manager.getAmtGhosts();
                        manager.addGhost(ghostObject);
                    }
                }
            }

            /*
             * else if (currPhase == GameplayManager.GamePhase.P1Seeking || currPhase == GameplayManager.GamePhase.P2Seeking)
             * {
             *  if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
             *  {
             *      Debug.Log(currPhase);
             *      Trackable trackable = hit.Trackable;
             *      if (trackable != null)
             *      {
             *          List<Anchor> anchors = new List<Anchor>();
             *          trackable.GetAllAnchors(anchors);
             *          bool foundGhost = false;
             *          foreach (Anchor anchor in anchors)
             *          {
             *              //Ghost is found
             *              if (foundGhost)
             *              {
             *                  break;
             *              }
             *              int anchorChildren = anchor.transform.childCount;
             *              for (int i = 0; i < anchorChildren; i++)
             *              {
             *                  GameObject child = anchor.transform.GetChild(i).gameObject;
             *                  if (child.name.Contains("Ghost"))
             *                  {
             *                      manager.removeGhost(child);
             *                      manager.poofFunc(hit.Pose.position, hit.Pose.rotation);
             *                      foundGhost = true;
             *                      Debug.Log("Found him");
             *                  }
             *              }
             *              Destroy(anchor);
             *          }
             *      }
             *  }
             * }
             */
        }