Esempio n. 1
0
    private void State()
    {
        if (health < 1)
        {
            Die();
            return;
        }

        if (manualOverride)
        {
            manualTimer += Time.deltaTime;

            if (manualTimer > manualTimerDelay)
            {
                if (!navMeshAgent.pathPending)
                {
                    if (navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
                    {
                        manualOverride = false;
                        manualTimer    = 0.0f;
                    }
                }
            }
        }

        if (navMeshAgent.velocity.magnitude > 0)
        {
            if (state != STATE.MOVING)
            {
                state = STATE.MOVING;
            }
        }

        if (target)
        {
            if (Vector3.Distance(transform.position, target.transform.position) < range)
            {
                if (state != STATE.FIGHTING)
                {
                    state = STATE.FIGHTING;
                    unitAnimator.SetFighting(true);
                }
            }
        }

        //else if (targetBuilding)
        //{
        //    if (Vector3.Distance(transform.position, hit.position) < range)
        //    {
        //        if (state != STATE.FIGHTING)
        //        {
        //            state = STATE.FIGHTING;
        //            unitAnimator.SetFighting(true);
        //        }
        //    }
        //}
        //else if (targetBuilding)
        //{
        //    RaycastHit hit1;
        //    Debug.DrawLine(transform.position, targetBuilding.transform.position);
        //    if (Physics.Raycast(transform.position, targetBuilding.transform.position - transform.position, out hit1, Mathf.Infinity))
        //    {
        //        if (hit1.collider.gameObject == targetBuilding.gameObject)
        //        {
        //            NavMeshHit hit;
        //            NavMesh.Raycast(transform.position, targetBuilding.transform.position, out hit, NavMesh.AllAreas);
        //            NavMesh.SamplePosition(hit.position, out hit, 20.0f, NavMesh.AllAreas);

        //            Debug.DrawRay(hit.position, Vector3.up, Color.red);

        //            if (Vector3.Distance(transform.position, hit.position) < range)
        //            {
        //                if (state != STATE.FIGHTING)
        //                {
        //                    state = STATE.FIGHTING;
        //                    unitAnimator.SetFighting(true);
        //                }
        //            }
        //        }
        //        else
        //        {
        //            NewDestination(targetBuilding.transform.position, false);
        //        }
        //    }
        //}
    }