Using the CrowdAgent component

The CrowdAgent component is incredibly simple, but gets the job done. As mentioned earlier, Unity does most of the heavy lifting for us. The following code gives our CrowdAgent a destination:

using UnityEngine; 
using System.Collections; 
 
[RequireComponent(typeof(NavMeshAgent))] 
public class CrowdAgent : MonoBehaviour {         
    public Transform target; 
 
    private NavMeshAgent agent; 
 
    void Start ()     { 
        agent = GetComponent<NavMeshAgent>(); 
        agent.speed = Random.Range(4.0f, 5.0f); 
        agent.SetDestination(target.position); 
    } 
}

The script requires a component of type NavMeshAgent, which it assigns to the agent variable on Start(). We then set its speed randomly between two values for some added visual variety in our simulation. Lastly, ...

Get Unity 2017 Game AI Programming - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.