Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (!thisStatus.dead)
        {
            if (!thisStatus.wipped)
            {
                if (canAttackPlayer)
                {
                    enemyAnimController.SetDirection((player.transform.position - transform.position).normalized);

                    if (Time.time >= nextTimeAttack)
                    {
                        AttackPlayer(player.transform.position, out bool attacked);
                    }
                }
            }
        }
        else
        {
            enemyAnimController.Walking(false);
            isAttacking     = false;
            canAttackPlayer = false;
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!thisStatus.dead)
        {
            if (!thisStatus.wipped)
            {
                canAttackPlayer = Mathf.Abs((player.transform.position - transform.position).magnitude) <= attackRange;
                if (!canAttackPlayer && oldCanAttack)
                {
                    canChasePlayer = true;
                    searchNewPath  = true;
                }
                if (canChasePlayer && !canAttackPlayer)
                {
                    ChasePlayer();
                    if (nextCheckT < Time.time)
                    {
                        nextCheckT += 0.5f;
                        if (Mathf.Abs(Mathf.Abs(oldCheckPos.x) - Mathf.Abs(transform.position.x)) <= 0.15f ||
                            Mathf.Abs(Mathf.Abs(oldCheckPos.y) - Mathf.Abs(transform.position.y)) <= 0.21f)
                        {
                            //Debug.Log("Stuck");
                            enemyAnimController.Walking(false);
                            velocity      = oldVel;
                            searchNewPath = true;
                        }
                        else
                        {
                            oldCheckPos = transform.position;
                            enemyAnimController.Walking(true);
                            Vector3 dir = (player.transform.position - transform.position).normalized;
                            enemyAnimController.SetDirection(velocity);
                            oldVel = velocity;
                        }
                    }
                }
                else if (canAttackPlayer)
                {
                    enemyAnimController.Walking(false);
                    Vector3 dir = (player.transform.position - transform.position).normalized;
                    enemyAnimController.SetDirection(dir);
                    currentTimeBtwAttack += Time.deltaTime;
                    if (currentTimeBtwAttack >= timeBtwAttack)
                    {
                        AttackPlayer(player.transform.position, out bool attacked);
                        currentTimeBtwAttack = attacked ? 0.0f : (currentTimeBtwAttack - Time.deltaTime);
                    }
                }
            }

            oldPlayerPos = player.transform.position;
            oldCanAttack = canAttackPlayer;
        }
        else
        {
            enemyAnimController.Walking(false);
            isAttacking     = false;
            canAttackPlayer = false;
            canChasePlayer  = false;
        }
    }