Exemple #1
0
    private void Awake()
    {
        gm = GameObject.FindGameObjectWithTag("GameManager").GetComponent <GameManager>();
        tm = gm.GetComponentInChildren <TeamManager>();

        life     = this.GetComponent <Unit_Life>();
        stats    = this.gameObject.GetComponent <Unit_Statistics>();
        attack   = this.gameObject.GetComponent <Unit_Attack>();
        movement = this.gameObject.GetComponent <Unit_Movement>();
    }
    public IEnumerator attackRoutine(GameObject target)
    {
        Unit targetUnit = target.GetComponent <Unit>();

        if (targetUnit != null)
        {
            Unit_Life targetLife = targetUnit.GetComponent <Unit_Life>();

            if (targetLife != null)
            {
                attacking = true;

                while (attacking)
                {
                    //print("Target is alive");

                    // Look at target
                    this.transform.LookAt(target.transform);

                    if (inRange.Contains(target))
                    {
                        //print("Target in range, attacking: " + target.name);

                        // Stop moving
                        unit.agent.SetDestination(unit.transform.position);

                        // SFX
                        // ...

                        // UnitType specifics
                        switch (unit.unitType)
                        {
                        case UnitType.GroundRanged:
                            CreateMuzzleFlash();
                            break;
                        }


                        // Target take damage
                        targetLife.TakeDamage((int)unit.stats.unitBaseDamage);

                        yield return(new WaitForSeconds(1));
                    }
                    else
                    {
                        while (attacking && !inRange.Contains(target))
                        {
                            //print("Moving to target");

                            // Move to target
                            try
                            {
                                unit.agent.SetDestination(target.transform.position);
                            }
                            catch (MissingReferenceException ex)
                            {
                                attacking = false;
                            }

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

                    if (targetLife.lifeCurrent > 0)
                    {
                        //print("Target still alive");
                        attacking = true;
                    }
                    else
                    {
                        //print("Target dead.");
                        attacking = false;
                    }
                }
            }
            else
            {
                Debug.LogWarning("Couldnt find UnitLife script on attack target: " + targetUnit.name);
            }
        }
        //print("Coroutine complete");
        yield return(null);
    }