Has Reached Destination
This checks if the NavMeshAgent has reached its destination.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
if (agent.HasReachedDestination())
{
Debug.Log("The agent has reached its destination.");
}
Avoid Obstacles
This adjusts the stopping distance based on obstacle detection.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
LayerMask obstacleLayer = LayerMask.GetMask("Obstacle");
agent.AvoidObstacles(rayDistance: 2f, layerMask: obstacleLayer, stoppingDistance: 1f);
Can See Target
This checks if the agent can see a target based on raycasting and field of view.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
Transform target = GameObject.Find("Target").transform;
if (agent.CanSeeTarget(target, rayDistance: 10f, fieldOfView: 60f))
{
Debug.Log("The agent can see the target.");
} else {
Debug.Log("The agent cannot see the target.");
}
Can Detect Target
This checks if the agent can detect the target within a specified range.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
Transform target = GameObject.Find("Target").transform;
if (agent.CanDetectTarget(target, detectionRange: 15f))
{
Debug.Log("The agent can detect the target.");
} else {
Debug.Log("The agent cannot detect the target.");
}
Seek
This moves the agent toward a target position by calculating a steering force based on a maximum speed.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
Vector3 targetPosition = new Vector3(10, 0, 10);
Vector3 steering = agent.Seek(targetPosition, maxSpeed: 5f);
Debug.Log("Calculated steering force: " + steering);
Avoid
This calculates the steering force to avoid obstacles around the agent.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
LayerMask obstacleLayer = LayerMask.GetMask("Obstacles");
Vector3 avoidance = agent.Avoidance(obstacleLayer, avoidDistance: 5f);
Debug.Log("Calculated avoidance force: " + avoidance);
Get Flocking Steering
This calculates the steering force for flocking behavior based on neighboring agents' positions and velocities.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
List<Transform> neighbors = GetNeighbors();
Vector3 flockingSteering = agent.GetFlockingSteering(neighbors, cohesionWeight: 1f, alignmentWeight: 1f, separationWeight: 1.5f);
Debug.Log("Calculated flocking steering force: " + flockingSteering);