Ejemplo n.º 1
0
    IEnumerator UpdateMovement()
    {
        while (true)
        {
            playersWithinRange = Physics.OverlapSphere(transform.position, aggroRange, layerMask);
            if (playersWithinRange.Length != 0)
            {
                if (aggroPosition == Vector3.zero)
                {
                    aggroPosition = transform.position;
                    myAgent.speed = 10;
                    myAnimator.SetBool("walk", false);
                    myAnimator.SetBool("run", true);
                }
                else if (myAgent.remainingDistance < 5f)
                {
                    myAnimator.SetBool("walk", false);
                    myAnimator.SetBool("run", false);
                }
                else
                {
                    myAnimator.SetBool("walk", false);
                    myAnimator.SetBool("run", true);
                }
                myAgent.destination = playersWithinRange [0].transform.position;
                Target = playersWithinRange [0].gameObject;
            }
            else if (myAgent.remainingDistance < 5f && aggroPosition == Vector3.zero)
            {
                myAgent.speed = 3.5f;
                myAnimator.SetBool("walk", true);
                myAnimator.SetBool("run", false);
                myAgent.destination = unitSpawner.RandomPositionOnMesh();
            }
            else if (aggroPosition != Vector3.zero)
            {
                playersWithinRange = null;
                myAgent.speed      = 20;
                myAnimator.SetBool("walk", false);
                myAnimator.SetBool("run", true);
                myAgent.destination = aggroPosition;
                aggroPosition       = Vector3.zero;
                while (true)
                {
                    yield return(new WaitForSeconds(0.1f));

                    if (myAgent.remainingDistance < 5f)
                    {
                        myAgent.speed = 3.5f;
                        myAnimator.SetBool("walk", true);
                        myAnimator.SetBool("run", false);
                        myAgent.destination = unitSpawner.RandomPositionOnMesh();
                        break;
                    }
                }
            }

            yield return(new WaitForSeconds(0.1f));
        }
    }