Esempio n. 1
0
    // spawn an animal in the
    void Spawn()
    {
        Vector2 position = CoordFinder.SearchCoord(transform, topCorner, bottomCorner, rightCorner, leftCorner);

        // spawn animal
        Instantiate(animals[Random.Range(0, 3)], new Vector3(position.x, 0.5f, position.y), Quaternion.identity, transform);
        currentTime = spawnTime + Time.time;
    }
Esempio n. 2
0
    protected IEnumerator MovementRoutine()
    {
        while (true)
        {
            //animal just spawned or stopped. Wait some times
            yield return(new WaitForSeconds(idleTime));

            //choose destination
            bool    isDistant = false;
            Vector2 coords    = Vector2.zero;
            while (!isDistant)
            {
                coords = CoordFinder.SearchCoord(transform.parent, spawner.topCorner, spawner.bottomCorner, spawner.rightCorner, spawner.leftCorner);
                if (Vector2.Distance(coords, new Vector2(transform.position.x, transform.position.z)) >= minimumDistance)
                {
                    isDistant = true;
                }
                yield return(new WaitForSeconds(thinkingTime));
            }
            destination = new Vector3(coords.x + transform.parent.position.x, transform.position.y, coords.y + transform.parent.position.z);
            //start moving to destination
            animator.SetBool("Move", true);
            navigator.enabled = true;
            yield return(new WaitForSeconds(0f));

            navigator.SetDestination(destination);
            //Debug.Log("Destination: "+destination);
            yield return(new WaitForSeconds(thinkingTime));

            //check when we arrive at the destination
            bool arrived = false;
            while (!arrived)
            {
                if (System.Math.Abs(destination.x - transform.position.x) < EPSILON && System.Math.Abs(destination.z - transform.position.z) < EPSILON)
                {
                    arrived = true;
                }
                yield return(new WaitForSeconds(thinkingTime));
            }
            //Debug.Log("Arrived");
            //destination reached. Stop animation
            animator.SetBool("Move", false);
            //stop moving because i'm arrived
            navigator.enabled = false;
        }
    }