Ejemplo n.º 1
0
    void Awake()
    {
        m_Health         = 100;
        m_EnemySFX       = GetComponent <EnemySFX>();
        m_EnemyAI        = GetComponent <EnemyAI>();
        m_EnemyAnimation = GetComponent <EnemyAnimation>();
        m_EnemyVFX       = GetComponent <EnemyVFX>();

        m_EnemyStateMachine = new StateMachine();

        m_IdleState   = new IdleState(m_EnemyAI.GetNavMeshAgent(), this.gameObject, m_EnemyAnimation.GetAnimator());
        m_ChaseState  = new ChaseState(this.gameObject, m_EnemyAI.GetNavMeshAgent(), m_EnemyAI.GetPlayer(), m_EnemyAnimation.GetAnimator());
        m_AttackState = new AttackState(this.gameObject, m_EnemyAnimation.GetAnimator());

        m_Player = GameObject.FindGameObjectWithTag("Player").GetComponent <Player>();
    }
Ejemplo n.º 2
0
    public virtual void Move()
    {
        if (Alive)
        {
            if (!Attacking)
            {
                if (isHit == false)
                {
                    //get x and y differences between this and player
                    //whichever one is larger, move in that direction (so it doesn't move directly diagonal
                    //needs to be a way to handle if the distances are exactly equal, if so favour x over y movement

                    //if moving
                    Vector3 posDif = transform.position - playerPosition;
                    if (playerPosition.x > transform.position.x) //if player is on right of enemy
                    {
                        flip = true;
                    }
                    else
                    {
                        flip = false;
                    }

                    if ((Vector3.Distance(transform.position, playerPosition)) <= aggroDistance)
                    {
                        if ((Vector3.Distance(transform.position, playerPosition)) <= 1.5f) //if in attacking range
                        {
                            Attack();
                        }
                        else    //if out of attacking range
                        {
                            _enemyAnim.GetAnimator().SetBool("InCombat", false);
                        }


                        if ((Mathf.Abs(transform.position.x - playerPosition.x)) >= Mathf.Abs((transform.position.y - playerPosition.y))) // if the distance between the x components is larger than the y components
                        {
                            posDif.y           = transform.position.y;                                                                    //basically, moving in a straight line horizontally
                            posDif.x           = playerPosition.x;
                            transform.position = Vector3.MoveTowards(transform.position, posDif, speed * Time.deltaTime);
                            _enemyAnim.Move(0.2f);
                        }
                        else //if the distance between the y components is larger than the x components
                        {
                            posDif.x           = transform.position.x; //basically, moving in a straight line vertically
                            posDif.y           = playerPosition.y;
                            transform.position = Vector3.MoveTowards(transform.position, posDif, speed * Time.deltaTime);
                            _enemyAnim.Move(0.2f);
                        }



                        //transform.position = Vector3.MoveTowards(transform.position, playerPosition, speed * Time.deltaTime);
                        //_enemyAnim.Move(0.2f);
                    }
                    else //meaning, out of range
                    {
                        _enemyAnim.GetAnimator().SetBool("InCombat", false);
                        _enemyAnim.Move(0);
                    }
                }
                Flip();
                //else, _enemyAnim.Move(0)
            }
        }
    }