read

4 Actor AI

  • Find source project Tutorial_3_AI_2 in Shooter

The logic

The enemy patrols in a particular path. When he sees the player, he would try to chase him. If he can’t catch with the player, he would go back to patrol.

The sight of enemy

Create an empty game object named EnemySight. Add a sphere collider to it, adjust the radius, and set it as Trigger.

Set the sight game object as the child of the enemy. Give it a script named EnemySight. The script can judge whether the player is inside the sight of the enemy or not. In Enemy script, it will be used as the option if the enemy chase the player or patrol.

// This is a collider trigger as the child of the enemy
public class EnemySight : MonoBehaviour
{
    public float fieldOfViewAngle = 110f;           // Number of degrees, centred on forward, for the enemy see.
    public bool playerInSight;                      // Whether or not the player is currently sighted.
    public Vector3 personalLastSighting;            // Last place this enemy spotted the player.
    public static Vector3 resetPos = Vector3.back;
    private GameObject player;                      // Reference to the player.

    void Awake()
    {
        player = GameObject.FindGameObjectWithTag("Player"/*Tags.player*/);

        // Set the personal sighting and the previous sighting to the reset position.
        personalLastSighting = resetPos;
    }

    void OnTriggerStay(Collider other)
    {
        // If the player has entered the trigger sphere...
        if (other.gameObject == player)
        {
            // By default the player is not in sight.
            playerInSight = false;

            // Create a vector from the enemy to the player and store the angle between it and forward.
            Vector3 direction = other.transform.position - transform.position;
            float angle = Vector3.Angle(direction, transform.forward);

            // If the angle between forward and where the player is, is less than half the angle of view...
            if (angle < fieldOfViewAngle * 0.5f)
            {
                RaycastHit hit;

                // ... and if a raycast towards the player hits something...
                if (Physics.Raycast(transform.position + transform.up, direction.normalized, out hit, col.radius))
                {
                    // ... and if the raycast hits the player...
                    if (hit.collider.gameObject == player)
                    {
                        // ... the player is in sight.
                        playerInSight = true;

                        // Set the last global sighting is the players current position.
                        //lastPlayerSighting.position = player.transform.position;

                        personalLastSighting = player.transform.position;

                    }
                }
            }
        }
    }

    void OnTriggerExit(Collider other)
    {
        // If the player leaves the trigger zone...
        if (other.gameObject == player)
            // ... the player is not in sight.
            playerInSight = false;
    }
}

Patrol

In enemy behavior script, EnemyPatrol, use enemysight to control the enemy patrol status. Set stopping distance in NavMeshAgent component inspection to a acceptable value.

The statuses of the enemy can be switched between patrol, chase, and other behaviors. Use sight, distance to the player, and other parameters to determine the status.

Next turoial please click Weapon and other advanced game functions.

More details, please visit the project here

Blog Logo

Honglei Han 韩红雷


Published

Image

Honglei Han 韩红雷的主页

Honglei Han from CUC 中国传媒大学 游戏设计 韩红雷 科研信息 所带课程 个人介绍 开源项目 创作的部分古体诗 Unity 3D 游戏引擎 Game Engine 游戏开发 计算机图形学 虚拟现实 Virtual Reality 程序设计 Programming

Back to Overview