The first thing to understand is that the enemy can find the protagonist through two forms: one is to see the protagonist (that is, the protagonist appears in the enemy's vision). The other is to hear the protagonist (that is, to hear the protagonist walking or running sound)
The first form: look.
For example, to determine whether the protagonist is in the enemy's visual angle, only to determine whether B<0.5*A is established
Second form, listen.
Using Unityengine;
Using System.Collections;
Using Unityengine.ai;
public class Enemysight:monobehaviour {
Private float seeangle=120;//Enemy vision Angle
private bool Isseeplay = false;
Private Vector3 lastpos;//player's last position
Private Vector3 Alermpos=vector3.zero; Alert location
Private Animator Anim; Protagonist animation, the role is to determine whether the protagonist in motion
Private Spherecollider spherecollider;//The enemy body, the collider is used to trigger the detection of the protagonist is visible in the enemy, can be heard within the range
Private Navmeshagent navmeshagent; AI components
void Awake ()
{
Anim = Gameobject.findgameobjectwithtag (Tags.player). Getcomponent<animator> ();
Lastpos = gamecontroller._instance.lastplayerpostion;
Navmeshagent = getcomponent<navmeshagent> ();
}
void Update ()
{
Sync Lead Location
if (lastpos! = gamecontroller._instance.lastplayerpostion)//change player position after triggering alarm
{
Alermpos = gamecontroller._instance.lastplayerpostion; Update alert Location
Lastpos = gamecontroller._instance.lastplayerpostion;
}
}
void Ontrigglestay (Collider other)
{
if (Other.tag==tags.player)
{
Watch players
Vector3 Startdir = transform.forward;//The enemy begins to move toward
Vector3 currdir = other.transform.position-transform.position; Vector of enemies looking into the player
float angle = Vector3.angle (Startdir, currdir);//The enemy begins to face the angle that the player is facing
if (Angle < Seeangle * 0.5f)//view angle is less than half visible
{
The protagonist is within the enemy's field of vision
Isseeplay = true;
Alermpos = other.transform.position;//The position of the playing home is set to the alert location
Gamecontroller._instance. Seeplayer (Other.transform);
}
Else
{
Isseeplay = false;
}
Listen to the sound of footsteps
if (Anim. Getcurrentanimatorstateinfo (0). Isname ("Locomotion"))//If the player is in motion
{
Navmeshpath path = new Navmeshpath ();
if (Navmeshagent.calculatepath (other.transform.position, Path))
{
vector3[] waypoints = new vector3[path.corners.length+2];
Waypoints[0] = transform.position;
Waypoints[waypoints.length-1] = other.transform.position;
for (int i = 0; i < path.corners.Length; i++)
{
Waypoints[i + 1] = path.corners[i];
}
float length = 0;
for (int i = 1; i < waypoints.length; i++)
{
Length + = (Waypoints[i]-waypoints[i-1]). magnitude; Total length of the polyline to which all nodes are connected
}
if (length <= Spherecollider.radius)//within the hearing range
{
Alermpos = other.transform.position;
}
}
}
}
}
void Ontriggleexit (Collider other)
{
if (Other.tag = = Tags.player)
{
Isseeplay = false;
}
}
Unity_ Practical Tips (enemies follow the lead)