Пример #1
0
    void SpawnFoodInstance() //edamas senes
    {
        GameObject foodItem = foodModels[Random.Range(0, foodModels.Length)];

        // Pick a location.  This is done by selecting a vertex at random and then
        // a random point between it and the center of the plane.
        List <Vector3> vertices = new List <Vector3>();

        detectedPlane.GetBoundaryPolygon(vertices);
        Vector3 pt       = vertices[Random.Range(0, vertices.Count)];
        float   dist     = Random.Range(0.05f, 1f);
        Vector3 position = Vector3.Lerp(pt, detectedPlane.CenterPose.position, dist);

        // Move the object above the plane.
        position.y += .05f;


        Anchor anchor = detectedPlane.CreateAnchor(new Pose(position, Quaternion.identity));

        foodInstance = Instantiate(foodItem, position, Quaternion.identity,
                                   anchor.transform);

        // Set the tag.
        foodInstance.tag = "food";

        foodInstance.transform.localScale = new Vector3(.025f, .025f, .025f);
        foodInstance.transform.SetParent(anchor.transform);
        foodAge = 0;

        /////////Liek senem rotet pa savu Y asi
        //foodInstance.AddComponent<FoodMotion>();
    }
Пример #2
0
    void CreateAnchor()
    {
        // Create the position of the anchor by raycasting a point towards
        // the top of the screen.
        // Vector2 pos = new Vector2(Screen.width * .5f, Screen.height * .90f);
        // Ray ray = firstPersonCamera.ScreenPointToRay(pos);
        // Vector3 anchorPosition = ray.GetPoint(5f);

        // Create the anchor at that point
        anchor = detectedPlane.CreateAnchor(
            new Pose(firstPersonCamera.transform.position, Quaternion.identity));

        plane = GameObject.Instantiate(interactivePlanePrefab, anchor.transform.position, anchor.transform.rotation, anchor.transform);

        if (index < 5)
        {
            planes[index] = plane;

            index = index + 1;
        }



        // Record the y offset from the plane.
        yOffset = plane.transform.position.y - detectedPlane.CenterPose.position.y;
    }
Пример #3
0
    private void createAnchor(TrackableHit spawnPoint, Vector3 position, Quaternion rotation)
    {
        anchor = (detectedPlane.CreateAnchor(
                      new Pose(spawnPoint.Pose.position, rotation)));

        if (room != null)
        {
            DestroyImmediate(room);
        }

        //Vector3 negativeAmount = roomPrefab.transform.Find("SpawnPoint").position;
        //float distance = Vector3.Distance(roomPrefab.transform.Find("SpawnPoint").position, roomPrefab.transform.Find("FloorForMeasurement").position);
        //Debug.Log(distance);
        //Vector3 newPosition = position-negativeAmount;
        room = Instantiate(TestRoom(), position, rotation);

        room.transform.SetParent(anchor.transform);

        /*
         * GameObject marker = GameObject.Find("PlacementMarkerController");
         * marker.SetActive(false);
         */
        GameObject planeGenerator = GameObject.Find("Plane Generator");

        planeGenerator.SetActive(false);
    }
Пример #4
0
    void CreateAnchor(Anchor imageAnchor)
    {
        // WITH AUGMENTED IMAGE

        Vector3 anchorPosition = imageAnchor.transform.position;

        if (_anchor != null)
        {
            DestroyObject(_anchor);
        }
        _anchor = _detectedPlane.CreateAnchor(new Pose(anchorPosition, Quaternion.identity));

        //Attach the anchorobject to the anchor
        transform.position = anchorPosition;                //lies on empty GameObject
        transform.forward  = imageAnchor.transform.forward; //aligns both Anchor forward vectors
        transform.SetParent(_anchor.transform);

        // Record the y offset from the plane.
        _yOffset = transform.position.y - _detectedPlane.CenterPose.position.y;

        // Enable the renderers.
        foreach (Renderer r in GetComponentsInChildren <Renderer>())
        {
            r.enabled = true;
        }

        _createdAnchor = true;
        ReadCamTransform();

        DrawCoordinates();
    }
Пример #5
0
    void CreateAnchor()
    {
        // Create the position of the anchor by raycasting a point towards
        // the top of the screen.
        Vector2 pos            = new Vector2(Screen.width * .5f, Screen.height * .90f);
        Ray     ray            = Camera.main.ScreenPointToRay(pos);
        Vector3 anchorPosition = ray.GetPoint(5f) + (Vector3.up * .7f); //spawns star above plane, not on it

        // Create the anchor at that point.
        if (anchor != null)
        {
            Destroy(anchor);
        }

        starPose = new Pose(anchorPosition, Quaternion.identity);
        anchor   = detectedPlane.CreateAnchor(starPose);

        // Attach the star to the anchor.
        transform.position = anchorPosition;
        transform.SetParent(anchor.transform);

        // Record the y offset from the plane.
        yOffset = transform.position.y - detectedPlane.CenterPose.position.y;

        transform.LookAt(cam);    //look at camera
        mRenderer.enabled = true; // enable the renderer, halo
        halo.enabled      = true;
        starEnabled       = true; //tell other scripts that star has been anchored
    }
Пример #6
0
    /// <summary>
    /// Spawns the food instance.
    /// </summary>
    private void SpawnFoodInstance()
    {
        GameObject foodItem = foodModels[Random.Range(0, foodModels.Length)];

        // Pick a location.  This is done by selecting a vertex at random and then
        // a random point between it and the center of the plane.
        List <Vector3> vertices = new List <Vector3>();

        detectedPlane.GetBoundaryPolygon(vertices);
        Vector3 pt       = vertices[Random.Range(0, vertices.Count)];
        float   dist     = Random.Range(0.05f, 1f);
        Vector3 position = Vector3.Lerp(pt, detectedPlane.CenterPose.position, dist);

        // Move the object above the plane.
        position.y += .05f;

        // Create an ARCore anchor for this position.
        Anchor anchor = detectedPlane.CreateAnchor(new Pose(position, Quaternion.identity));

        // Create the instance.
        foodInstance = Instantiate(foodItem, position, Quaternion.identity, anchor.transform);

        // Set the tag - IMPORTANT: make sure the tag is defined in the Tag Editor.
        foodInstance.tag = "food";

        foodInstance.transform.localScale = new Vector3(.025f, .025f, .025f);
        foodInstance.transform.SetParent(anchor.transform);
        foodAge = 0;

        foodInstance.AddComponent <FoodMotion>();
    }
Пример #7
0
    void SpawnPelletInstance()
    {
        GameObject pelletItem = pelletModel[Random.Range(0, pelletModel.Length)];

        // Pick a location.  This is done by selecting a vertex at random and then
        // a random point between it and the center of the plane.
        List <Vector3> vertices = new List <Vector3>();

        detectedPlane.GetBoundaryPolygon(vertices);
        Vector3 pt       = vertices[Random.Range(0, vertices.Count)];
        float   dist     = Random.Range(0.05f, 1f);
        Vector3 position = Vector3.Lerp(pt, detectedPlane.CenterPose.position, dist);

        // Move the object above the plane.
        position.y += .05f;


        Anchor anchor = detectedPlane.CreateAnchor(new Pose(position, Quaternion.identity));

        pelletInstance = Instantiate(pelletItem, position, Quaternion.identity,
                                     anchor.transform);

        // Set the tag.
        pelletInstance.tag = "pellet";

        pelletInstance.transform.localScale = new Vector3(.025f, .025f, .025f);
        pelletInstance.transform.SetParent(anchor.transform);
        pelletAge = 0;

        pelletInstance.AddComponent <FoodMotion>();
    }
Пример #8
0
    /// <summary>
    /// Create the position of the anchor by raycasting a point towards
    /// the top of the screen
    /// </summary>
    void CreateAnchor()
    {
        Vector2 pos            = new Vector2(Screen.width * .5f, Screen.height * .90f);
        Ray     ray            = firstPersonCamera.ScreenPointToRay(pos);
        Vector3 anchorPosition = ray.GetPoint(5f);

        // Create the anchor at that point.
        if (anchor != null)
        {
            DestroyObject(anchor);
        }

        anchor = detectedPlane.CreateAnchor(
            new Pose(anchorPosition, Quaternion.identity));

        // Attach the nutrition panel to the anchor.
        transform.position = anchorPosition;
        transform.SetParent(anchor.transform);
        yOffset = transform.position.y - detectedPlane.CenterPose.position.y;

        // Enable the renderers to display the nutrition panel.
        foreach (Renderer r in GetComponentsInChildren <Renderer>())
        {
            r.enabled = true;
        }
    }
Пример #9
0
    void CreateAnchor()
    {
        // Create the position of the anchor by raycasting a point towards
        // the top of the screen.
        Vector2 pos            = new Vector2(Screen.width * .5f, Screen.height * .90f);
        Ray     ray            = firstPersonCamera.ScreenPointToRay(pos);
        Vector3 anchorPosition = ray.GetPoint(5f);

        // Create the anchor at that point.
        if (anchor != null)
        {
            Destroy(anchor);
        }
        anchor = detectedPlane.CreateAnchor(
            new Pose(anchorPosition, Quaternion.identity));

        // Attach the scoreboard to the anchor.
        transform.position = anchorPosition;
        transform.SetParent(anchor.transform);

        // Record the y offset from the plane.
        yOffset = transform.position.y - detectedPlane.CenterPose.position.y;

        // Finally, enable the renderers.
        foreach (Renderer r in GetComponentsInChildren <Renderer>())
        {
            r.enabled = true;
        }
    }
Пример #10
0
    void SpawnBlinkly()
    {
        if (blinklyInstance != null)
        {
            DestroyImmediate(blinklyInstance);
        }


        List <Vector3> vertices = new List <Vector3>();

        detectedPlane.GetBoundaryPolygon(vertices);
        Vector3 pos      = vertices[Random.Range(0, vertices.Count)];
        float   dist     = Random.Range(0.05f, 1f);
        Vector3 position = Vector3.Lerp(pos, detectedPlane.CenterPose.position, dist);

        // Move the object above the plane.
        position.y += .05f;


        Anchor anchor = detectedPlane.CreateAnchor(new Pose(position, Quaternion.identity));

        // Not anchored, it is rigidbody that is influenced by the physics engine.
        blinklyInstance = Instantiate(blinklyPrefab, pos,
                                      Quaternion.identity, transform);
        // After instantiating a new snake instance, add the FoodConsumer component.
        blinklyInstance.AddComponent <PacmanController>();
    }
Пример #11
0
        /// <summary>
        /// The Unity Update() method.
        /// </summary>
        public void Update()
        {
            _UpdateApplicationLifecycle();

            // Hide snackbar when currently tracking at least one plane.
            Session.GetTrackables <DetectedPlane>(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 (!objectSpawned)
            {
                for (int i = 0; i < m_AllPlanes.Count; i++)
                {
                    DetectedPlane plane = m_AllPlanes[i];

                    if ((plane.TrackingState == TrackingState.Tracking) && (plane.PlaneType == DetectedPlaneType.HorizontalUpwardFacing))
                    {
                        // Calculate a random point in the plane to place our object. In order to do it, we first obtain
                        // the boundary points of the polygon. The we randomly select one of the points and use it together
                        // with the center point to get a line. Then we just randomly select one point from that line

                        List <Vector3> polyPoints = new List <Vector3>();
                        plane.GetBoundaryPolygon(polyPoints);

                        int rPointIndex = Random.Range(0, polyPoints.Count - 1);

                        Vector3 rPoint     = polyPoints[rPointIndex];
                        Vector3 polyCenter = plane.CenterPose.position;

                        // Linearly intepolate between the two points based on a random value (randomly select a point in the line)
                        Vector3 rMidPoint = Vector3.Lerp(rPoint, polyCenter, Random.Range(0.0f, 1.0f));

                        Pose rMidPointPose = new Pose(rMidPoint, plane.CenterPose.rotation);

                        // Instantiate the custom object in the calculated position
                        var customObj = Instantiate(ObjectPrefab, rMidPoint, plane.CenterPose.rotation);
                        var anchor    = plane.CreateAnchor(rMidPointPose);

                        customObj.transform.parent = anchor.transform;

                        objectSpawned = true;

                        break;
                    }
                }
            }
        }
Пример #12
0
    private void InitializeNewPlane(DetectedPlane detectedPlane)
    {
        var anchor = detectedPlane.CreateAnchor(detectedPlane.CenterPose);

        anchor.transform.SetParent(planeRoot.transform);
        var plane           = GameObject.Instantiate(planePrefab, anchor.transform);
        var planeController = plane.GetComponent <PlaneController>();

        planeController.Initialize(detectedPlane);

        var performerType = performerSelector.GetNextPerformerType();

        planeController.instrument.SetSample(performerType.sample);
        planeController.generator = GeneratorFactory.CreateGenerator(planeController.instrument,
                                                                     performerType.generatorName);


        // TEST //
        if (performerType.generatorName == "Drums")
        {
            var generator = planeController.generator as Drums;
            if (performerType.sample.name.Contains("kick"))
            {
                generator.drumType = DrumType.KICK;
            }
            else if (performerType.sample.name.Contains("snare"))
            {
                generator.drumType = DrumType.SNARE;
            }
            else if (performerType.sample.name.Contains("clap"))
            {
                generator.drumType = DrumType.CLAP;
            }
            else if (performerType.sample.name.Contains("closed"))
            {
                generator.drumType = DrumType.HH_CLOSED;
            }
            else if (performerType.sample.name.Contains("open"))
            {
                generator.drumType = DrumType.HH_OPEN;
            }
        }
        else if (performerType.generatorName != "SimpleBassline")
        {
            planeController.instrument.noteOffset = Random.Range(-8, 8); // noteOffsetGenerator.CurrentState;
            //noteOffsetGenerator.GenerateNextState();
        }

        if (performerType.generatorName == "OneBarNote")
        {
            (planeController.generator as OneBarNote).beatOffset = 0.5 * Random.Range(0, 8);
        }
        if (performerType.generatorName == "SimpleBassline")
        {
            (planeController.generator as SimpleBassline).beatOffset = Random.Range(0, 4);
        }
    }
Пример #13
0
    private void SpawnMarker(DetectedPlane detectedPlane)
    {
        marker = Instantiate(markerPrefab, detectedPlane.CenterPose.position, Quaternion.identity, this.transform);

        if (anchor == null)
        {
            anchor = detectedPlane.CreateAnchor(Pose.identity);
        }
        marker.transform.SetParent(anchor.transform);
    }
Пример #14
0
    public void AttachToAnchor(TrackableHit hit)
    {
        // if moving off plane
        if (hit.Trackable == null)
        {
            Debug.Log("Card off plane!");
            return;
        }

        // move to hit position
        transform.position = hit.Pose.position;

        // if moving to new plane
        if (hit.Trackable != detectedPlane)
        {
            Debug.Log("Moving Card to new plane!");
            detectedPlane = (DetectedPlane)hit.Trackable;
            return;
        }

        // if does not have an anchor or too far away from current anchor
        // give it an anchor
        if (anchor == null || (anchor.transform.position - transform.position).magnitude > 1)
        {
            // gets all existing anchors on this plane
            List <Anchor> existingAnchors = new List <Anchor>();
            detectedPlane.GetAllAnchors(existingAnchors);

            bool anchorFound = false;
            // look for a nearby existing anchor
            foreach (Anchor a in existingAnchors)
            {
                // if found a nearby anchor
                if ((a.transform.position - hit.Pose.position).magnitude < 1)
                {
                    Debug.Log("Moving card to existing anchor!");
                    anchor      = a;
                    anchorFound = true;
                    break;
                }
            }

            //if did not find an anchor, create a new one
            if (!anchorFound)
            {
                Debug.Log("Creating new anchor!");
                anchor = detectedPlane.CreateAnchor(hit.Pose);
            }

            transform.SetParent(anchor.transform);
        }

        //Debug.Log("Anchor Position: " + anchor.transform.position);
        //Debug.Log("Distance From Anchor: " + transform.localPosition.magnitude);
    }
Пример #15
0
 public void GenerateAnchorAndObject(DetectedPlane plane, Vector3 centerPlanePosition)
 {
     if (noPlaneExists == true)
     {
         noPlaneExists             = false;
         arObject                  = Instantiate(GameObjectPointPrefab, centerPlanePosition, transform.rotation);
         anchorObject              = plane.CreateAnchor(new Pose(centerPlanePosition, Quaternion.identity));
         arObject.transform.parent = anchorObject.transform;
         debugTextObject.GetComponent <Text>().text = "prefab generated";
     }
 }
Пример #16
0
    /// <summary>
    /// Clip Plane and Content to new Anchor
    /// </summary>
    private void ClipNewFloor()
    {
        UpdateFloor = false;

        for (int i = m_AllPlanes.Count - 1; i > 0; i--)
        {
            DetectedPlane  currentPlane  = m_AllPlanes[i];
            List <Vector3> polygonPoints = new List <Vector3>();
            currentPlane.GetBoundaryPolygon(polygonPoints);

            if (currentPlane.TrackingState == TrackingState.Tracking && currentPlane.PlaneType == DetectedPlaneType.HorizontalUpwardFacing && polygonPoints.Count > 5)
            {
                // Add a new Anchor
                var anchor = currentPlane.CreateAnchor(currentPlane.CenterPose);
                planeAnchors.Add(anchor);

                // Take all GameObjects from the previous Anchor to the new one
                Transform[] trans = planeAnchors[planeAnchors.Count - 2].GetComponentsInChildren <Transform>();
                foreach (Transform t in trans.Skip(1))
                {
                    // Make sure the Transform t is a direct child of the previous Anchor
                    if (t.parent.Equals(planeAnchors[planeAnchors.Count - 2].transform))
                    {
                        t.parent = anchor.transform;
                        Vector3 pos = new Vector3(t.position.x, currentPlane.CenterPose.position.y, t.position.z);
                        t.position = pos;
                    }
                }

                // Destroy old Anchor
                if (planeAnchors[0].GetComponentsInChildren <Transform>().Length < 2)
                {
                    planeAnchors[0].gameObject.transform.parent = null;
                    Destroy(planeAnchors[0].gameObject);
                    planeAnchors.RemoveAt(0);
                }

                break;
            }
        }

        UpdateFloor = true;
    }
Пример #17
0
    private void RenderCubes()
    {
        var xyList = ListOfCirclePoints(numberOfCubes, circleRadius, 0, 0);
        int i      = 0;

        foreach (Renderer cubePrefab in GetComponentsInChildren <Renderer>())
        {
            cubePrefab.enabled            = true;
            cubePrefab.material.color     = presetColor;
            cubePrefab.transform.position =
                new Vector3(xyList.ElementAt(i).Key, xyList.ElementAt(i).Value, 0);
            i++;
        }

        Pose newPose = new Pose(new Vector3(hit.Pose.position.x, 0, hit.Pose.position.z), hit.Pose.rotation);

        anchor = detectedPlane.CreateAnchor(newPose);
        // Attach the scoreboard to the anchor.
        transform.SetParent(anchor.transform);
    }
    void SpawnFoodInstance()
    {
        GameObject     foodItem = foodModels[Random.Range(0, foodModels.Length)];
        List <Vector3> vertices = new List <Vector3>();

        detectedPlane.GetBoundaryPolygon(vertices);
        Vector3 pt       = vertices[Random.Range(0, vertices.Count)];
        float   dist     = Random.Range(0.05f, 1f);
        Vector3 position = Vector3.Lerp(pt, detectedPlane.CenterPose.position, dist);

        position.y += .05f;


        Anchor anchor = detectedPlane.CreateAnchor(new Pose(position, Quaternion.identity));

        foodInstance     = Instantiate(foodItem, position, Quaternion.identity, anchor.transform);
        foodInstance.tag = "Food";
        foodInstance.transform.localScale = new Vector3(.025f, .025f, .025f);
        foodInstance.transform.SetParent(anchor.transform);
        foodAge = 0;
        foodInstance.AddComponent <FoodMotion>();
    }
Пример #19
0
    private void PlaceNodeObject()
    {
        // Place a node in front of user on top of the detected plane
        // Anchor above center of detected plane

        if (targetAnchor != null)
        {
            DestroyObject(targetAnchor);
        }

        targetAnchor = detectedPlane.CreateAnchor(detectedPlane.CenterPose);

        // Create and position empty object that will be populated with downloaded node cover
        targetNodeObj = new GameObject("NodeHolder");
        targetNodeObj.transform.SetParent(targetAnchor.transform);
        targetNodeObj.transform.localPosition = new Vector3(0f, NODE_PLACEMENT_HEIGHT, 0f);

        // Add a node controller to it - this will take care of the rest
        NodeController nodeController = targetNodeObj.AddComponent <NodeController>();

        nodeController.SetAndLoadNode(targetNode);
    }
    void CreateAnchor()
    {
        Vector2 pos            = new Vector2(Screen.width * .5f, Screen.height * .90f);
        Ray     ray            = firstPersonCamera.ScreenPointToRay(pos);
        Vector3 anchorPosition = ray.GetPoint(5f);

        if (anchor != null)
        {
            Object.Destroy(anchor);
        }
        anchor = detectedPlane.CreateAnchor(
            new Pose(anchorPosition, Quaternion.identity));

        transform.position = anchorPosition;
        transform.SetParent(anchor.transform);

        yOffset = transform.position.y - detectedPlane.CenterPose.position.y;

        foreach (Renderer r in GetComponentsInChildren <Renderer>())
        {
            r.enabled = true;
        }
    }
Пример #21
0
    /// <summary>
    /// Spawns enemies based on the given parameters
    /// </summary>
    /// <param name="maxSpawnDelay">Maximum time delay for next wave spawn, minimum is 1 second</param>
    /// <param name="maxNumberOfEnemies">Maximum number of enemies to spawn, minimum is 1 enemy</param>
    /// <returns></returns>
    IEnumerator SpawnDelayTimer(float maxSpawnDelay, int maxNumberOfEnemies)
    {
        float time = 0f;

        float spawnDelay = Random.Range(1f, maxSpawnDelay);

        int numberOfEnemies = Random.Range(1, maxNumberOfEnemies);

        // Wait for the spawn delay
        while (time < spawnDelay)
        {
            time += Time.deltaTime;
            yield return(null);
        }

        gamePlane = levelManager.GetComponent <LevelManager>().GetGamePlane();

        // If game has started and we have a plane, set the hut on the plane as the center
        // and spawn all enemies one by one
        if (gamePlane != null)
        {
            waveNumber++;
            UpdateWaveText();

            DetectedPlane gameDetectedPlane = gamePlane as DetectedPlane;

            Vector3 center = levelManager.GetComponent <LevelManager>().madeHut.transform.position;

            Pose enemyPose;

            for (int i = 0; i < numberOfEnemies; i++)
            {
                Vector3 pos = RandomCircle(center, 10.0f);

                Quaternion rot = Quaternion.FromToRotation(Vector3.forward, center - pos);

                enemyPose = new Pose(pos, rot);

                Debug.Log("enemy spawned at " + pos);

                GameObject newEnemy = Instantiate(prefab, pos, rot);

                newEnemy.GetComponent <EnemyMovement>().SetTargetHut(levelManager.GetComponent <LevelManager>().madeHut);

                AddEnemyToList(newEnemy);

#if UNITY_EDITOR
                Debug.Log("Enemy spawned in editor");
#else
                Debug.Log("Enemy spawned in android");
                var enemyAnchor = gameDetectedPlane.CreateAnchor(enemyPose);

                newEnemy.transform.parent = enemyAnchor.transform;
#endif
                newEnemy.GetComponent <EnemyMovement>().SetTargetHut(levelManager.GetComponent <LevelManager>().madeHut);
            }
        }

        // Start timer for the next batch of enemies
        StartCoroutine(SpawnDelayTimer(5f, 2));
    }
Пример #22
0
    /// <summary>
    /// The Unity Update method.
    /// </summary>
    public void Update()
    {
        // Exit the app when the 'back' button is pressed.
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }

        // The game plane is only set when the game has started
        if (levelManager.gamePlaneSet)
        {
            // Get updated augmented images for this frame.
            Session.GetTrackables <AugmentedImage>(
                m_TempAugmentedImages, TrackableQueryFilter.Updated);


            // Create visualizers and anchors for updated augmented images that are tracking and do
            // not previously have a visualizer. Remove visualizers for stopped images.
            foreach (var image in m_TempAugmentedImages)
            {
                EffectVisualizer visualizer = null;
                m_Visualizers.TryGetValue(image.DatabaseIndex, out visualizer);

                // Check for shield image and shield cooldown
                if (image.DatabaseIndex == 0 && canUseShield)
                {
                    // similar if statement for every spell. This checks if the spell marker is currently in the view and there isnt a spell already being run from the marker.
                    if (image.TrackingState == TrackingState.Tracking && visualizer == null && image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking)
                    {
                        // Get the game detected plane
                        DetectedPlane gameDetectedPlane = levelManager.GetGamePlane() as DetectedPlane;

                        // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                        var anchor = gameDetectedPlane.CreateAnchor(gameDetectedPlane.CenterPose);

                        // Set the effect position to the hut position for the shield spell
                        Vector3 effectPosition = levelManager.madeHut.transform.position;

                        // Make the visualizer and anchor it to the hut
                        visualizer = (EffectVisualizer)Instantiate(AugmentedImageVisualizerPrefab, effectPosition, levelManager.madeHut.transform.rotation);

                        visualizer.gameObject.transform.parent = anchor.transform;

                        Debug.Log("effect position is: " + effectPosition);

                        Debug.Log("image pose is: " + image.CenterPose);

                        visualizer.Image = image;

                        m_Visualizers.Add(image.DatabaseIndex, visualizer);

                        // We can not use shields anymore till the cooldown is up
                        canUseShield = false;

                        // Start shield cooldown
                        StartCoroutine(ShieldCooldownTimer());
                    }
                    // if  the image isn't being tracked and there is still a visualizer, destroy the visualizer
                    else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
                    {
                        m_Visualizers.Remove(image.DatabaseIndex);
                        GameObject.Destroy(visualizer.gameObject);
                    }
                }
                // Check for spike image and spike cooldown
                else if (image.DatabaseIndex == 1 && canUseSpikes)
                {
                    if (image.TrackingState == TrackingState.Tracking && visualizer == null && image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking)
                    {
                        // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                        var anchor = image.CreateAnchor(image.CenterPose);

                        DetectedPlane gameDetectedPlane = levelManager.GetGamePlane() as DetectedPlane;

                        Vector3 effectPosition = new Vector3(image.CenterPose.position.x, gameDetectedPlane.CenterPose.position.y, image.CenterPose.position.z);

                        visualizer = (EffectVisualizer)Instantiate(AugmentedImageVisualizerPrefab, effectPosition, image.CenterPose.rotation);

                        visualizer.gameObject.transform.parent = anchor.transform;

                        Debug.Log("effect position is: " + effectPosition);

                        Debug.Log("image pose is: " + image.CenterPose);

                        visualizer.Image = image;

                        m_Visualizers.Add(image.DatabaseIndex, visualizer);

                        canUseSpikes = false;

                        StartCoroutine(SpikesCooldownTimer());
                    }
                    else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
                    {
                        m_Visualizers.Remove(image.DatabaseIndex);
                        GameObject.Destroy(visualizer.gameObject);
                    }
                }
                // Check for ball image and ball cooldown
                else if (image.DatabaseIndex == 2 && canUseBall)
                {
                    if (image.TrackingState == TrackingState.Tracking && visualizer == null && image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking)
                    {
                        // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                        var anchor = image.CreateAnchor(image.CenterPose);

                        DetectedPlane gameDetectedPlane = levelManager.GetGamePlane() as DetectedPlane;

                        Vector3 effectPosition = new Vector3(image.CenterPose.position.x, gameDetectedPlane.CenterPose.position.y, image.CenterPose.position.z);

                        visualizer = (EffectVisualizer)Instantiate(AugmentedImageVisualizerPrefab, effectPosition, image.CenterPose.rotation);

                        visualizer.gameObject.transform.parent = anchor.transform;

                        Debug.Log("effect position is: " + effectPosition);

                        Debug.Log("image pose is: " + image.CenterPose);

                        visualizer.Image = image;

                        m_Visualizers.Add(image.DatabaseIndex, visualizer);

                        canUseBall = false;

                        StartCoroutine(BallCooldownTimer());
                    }
                    else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
                    {
                        m_Visualizers.Remove(image.DatabaseIndex);
                        GameObject.Destroy(visualizer.gameObject);
                    }
                }
                // Check for fire image and fire cooldown
                else if (image.DatabaseIndex == 3 && canUseFire)
                {
                    if (image.TrackingState == TrackingState.Tracking && visualizer == null && image.TrackingMethod == AugmentedImageTrackingMethod.FullTracking)
                    {
                        // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                        var anchor = image.CreateAnchor(image.CenterPose);

                        DetectedPlane gameDetectedPlane = levelManager.GetGamePlane() as DetectedPlane;

                        Vector3 effectPosition = new Vector3(image.CenterPose.position.x, gameDetectedPlane.CenterPose.position.y, image.CenterPose.position.z);

                        visualizer = (EffectVisualizer)Instantiate(AugmentedImageVisualizerPrefab, effectPosition, image.CenterPose.rotation);

                        visualizer.gameObject.transform.parent = anchor.transform;

                        Debug.Log("effect position is: " + effectPosition);

                        Debug.Log("image pose is: " + image.CenterPose);

                        visualizer.Image = image;

                        m_Visualizers.Add(image.DatabaseIndex, visualizer);

                        canUseFire = false;

                        StartCoroutine(FireCooldownTimer());
                    }
                    else if (image.TrackingState == TrackingState.Stopped && visualizer != null)
                    {
                        m_Visualizers.Remove(image.DatabaseIndex);
                        GameObject.Destroy(visualizer.gameObject);
                    }
                }
                else
                {
                    Debug.Log("image not in database");
                }
            }
        }
    }
Пример #23
0
    // Update is called once per frame
    void Update()
    {
        if (detectedPlane == null)
        {
            return;
        }

        if (detectedPlane.TrackingState != TrackingState.Tracking)
        {
            return;
        }

        // Check for the plane being subsumed
        // If the plane has been subsumed switch attachment to the subsuming plane.
        while (detectedPlane.SubsumedBy != null)
        {
            detectedPlane = detectedPlane.SubsumedBy;
        }

        if (foodInstance == null || foodInstance.activeSelf == false)
        {
            SpawnFoodInstance();
            return;
        }

        foodAge += Time.deltaTime;
        if (foodAge >= maxAge)
        {
            Destroy(foodInstance);
            foodInstance = null;
        }

        void SpawnFoodInstance()
        {
            GameObject foodItem = foodModels[Random.Range(0, foodModels.Length)];

            // Pick a location.  This is done by selecting a vertex at random and then
            // a random point between it and the center of the plane.
            List <Vector3> vertices = new List <Vector3>();

            detectedPlane.GetBoundaryPolygon(vertices);
            Vector3 pt       = vertices[Random.Range(0, vertices.Count)];
            float   dist     = Random.Range(0.05f, 1f);
            Vector3 position = Vector3.Lerp(pt, detectedPlane.CenterPose.position, dist);

            // Move the object above the plane.
            position.y += .05f;


            Anchor anchor = detectedPlane.CreateAnchor(new Pose(position, Quaternion.identity));

            foodInstance = Instantiate(foodItem, position, Quaternion.identity,
                                       anchor.transform);

            // Set the tag.
            foodInstance.tag = "food";

            foodInstance.transform.localScale = new Vector3(.025f, .025f, .025f);
            foodInstance.transform.SetParent(anchor.transform);
            foodAge = 0;

            foodInstance.AddComponent <FoodMotion>();
        }
    }