示例#1
0
    // Update is called once per frame
    void Update()
    {
        bool doesSeePlayer  = false;
        bool doesHearPlayer = false;

        // while the enemy isnt reloading
        if (lastShot + ReloadSpeed <= Time.time)
        {
            // Check
            float dist = Vector3.Distance(Player.transform.position, transform.position);

            //Can see \ hear player (depends of the facing (scale.x)
            if (dist <= SightRange &&
                ((transform.localScale.x < 0 && transform.position.x < Player.transform.position.x) ||
                 (transform.localScale.x > 0 && transform.position.x > Player.transform.position.x))
                )
            {
                collider.enabled = false;
                RaycastHit2D hit = Physics2D.Raycast(transform.position, Player.transform.position - transform.position);
                if (hit.collider != null)
                {
                    if (hit.collider.gameObject.Equals(Player.gameObject))
                    {
                        doesSeePlayer = true;
                    }
                }
                collider.enabled = true;
            }

            if (doesSeePlayer)
            {
                laserLineRenderer.enabled = true;

                // Attack
                lastShot = Time.time;
                PerformAttack();
            }
            else if (doesHearPlayer)
            {
                laserLineRenderer.enabled = false;
                // Move to position
                if (awareMovement != null)
                {
                    awareMovement.ExecuteMove(this);
                }
            }
            else
            {
                laserLineRenderer.enabled = false;
                // Idle
                if (awareMovement != null)
                {
                    idleMovement.ExecuteMove(this);
                }
            }
        }
    }