コード例 #1
0
ファイル: ChaseState.cs プロジェクト: Staz00/Breakpoint
    private void Look()
    {
        RaycastHit hit;

        if (enemy.target != null)
        {
            Vector3 enemyToTarget = (enemy.target.position + enemy.offset) - enemy.eyes.transform.position;

            //while this entity sees a player object, stay in this state otherwise return to alert state
            if (Physics.Raycast(enemy.eyes.transform.position, enemyToTarget, out hit, enemy.sightRange, enemy.playerMask) && hit.collider.CompareTag("Player"))
            {
                hasTarget = true;

                enemy.target = hit.transform;


                //if this entity is close enough to the target, then initiate attack
                if (Vector3.Distance(enemy.eyes.transform.position, hit.transform.position) <= 4f)
                {
                    m_Timer -= Time.deltaTime;

                    //Attack cooldown
                    if (m_Timer <= 0)
                    {
                        m_Timer = enemy.attackSpeed;

                        //rotate this entity towards its target
                        enemy.transform.rotation = Quaternion.Lerp(enemy.transform.rotation, Quaternion.LookRotation(hit.collider.transform.position - enemy.transform.position),
                                                                   Time.deltaTime * 20f);

                        enemy.DoDamage(hit);
                    }
                }
            }
            else
            {
                ToAlertState();
            }
        }
        else
        {
            ToAlertState();
        }
    }