Пример #1
0
    // Update is called once per frame
    void Update()
    {
        //Don't check for sight if the enemy is already alerted
        if (mind.state != EnemyMind.STATES.ALERT)
        {
            //check to see if the player is in the enemies line of sight
            bool seen = false;
            foreach (Character p in players)
            {
                //If the player is caught, no need to check them
                if (!p.isActivated)
                {
                    continue;
                }
                //if the player is within the cone of vision or they are too close to the guard, they can be seen
                Vector3 correctedPosition = new Vector3(p.transform.position.x, 1, p.transform.position.z);
                Vector3 dir       = correctedPosition - transform.position;
                float   angle     = Vector3.Angle(dir, transform.forward);
                float   pDistance = Vector3.Distance(correctedPosition, transform.position);
                if (angle <= visionThreshold / 2 || pDistance < SenseDistance)
                {
                    RaycastHit hit;
                    if (Physics.Raycast(transform.position, dir.normalized, out hit))
                    {
                        Debug.DrawLine(transform.position, hit.point);
                        if (hit.collider.tag == "Player")
                        {
                            hideTime -= Time.deltaTime + 1 / pDistance;

                            //If the player has been spotted too much
                            if (hideTime <= 0)
                            {
                                //Alert the guard
                                mind.alert(p);
                                hideTime = hideTimeMax;
                                return;
                            }
                            if (hideTime <= hideTimeMax / 2)
                            {
                                //Or investigate if not fully caught
                                mind.Investigate(p.gameObject.transform.position);
                            }
                        }
                    }
                    seen = true;
                    break;
                }
            }

            //if there is no player in sight
            if (!seen)
            {
                hideTime += Time.deltaTime / hideCooldown;
                hideTime  = Mathf.Clamp(hideTime, 0, hideTimeMax);
            }

            sightImage.color = Color.Lerp(Color.red, Color.clear, hideTime / hideTimeMax);
        }
    }