unity navmesh agent code example
Example: navmesh agent unity follow object
using UnityEngine;
using UnityEngine.AI;
public class MoveToTarget : MonoBehaviour
{
//Assign in inspector the target that you want to follow
public Transform target;
void Update()
{
//Move towards target every frame and stay in the NavMesh
GetComponent<NavMeshAgent>().destination = target.position;
}
}