I’m having an issue where an agent object has jerky movement along the y axis when manually traversing off mesh links. The object (a cylinder in this case) leaves the navmesh agent collider intermittently until control is handed back over to the navmesh agent.
Here’s the code:
public class NavAgentExample : MonoBehaviour
{
public AIWayPointNetwork waypointNetwork = null;
public int currentIndex = 0;
public bool hasPath = false;
public bool pathPending = false;
public bool isPathStale = false;
public NavMeshPathStatus pathStatus = NavMeshPathStatus.PathInvalid;
public AnimationCurve jumpCurve = new AnimationCurve();
private NavMeshAgent _navAgent;
private void Start()
{
_navAgent = GetComponent<NavMeshAgent>();
if (waypointNetwork == null)
{
return;
}
SetNextDestination(false);
}
void SetNextDestination(bool increment)
{
if(!waypointNetwork)
{
return;
}
int incStep = increment ? 1 : 0;
Transform nextWaypointTransform = null;
int nextWayPointIndex = (currentIndex + incStep >= waypointNetwork.wayPoints.Count) ? 0 : currentIndex+incStep;
nextWaypointTransform = waypointNetwork.wayPoints[nextWayPointIndex];
if (nextWaypointTransform != null)
{
currentIndex = nextWayPointIndex;
_navAgent.destination = nextWaypointTransform.position;
return;
}
currentIndex++;
}
private void Update()
{
hasPath = _navAgent.hasPath;
pathPending = _navAgent.pathPending;
isPathStale = _navAgent.isPathStale;
pathStatus = _navAgent.pathStatus;
if(_navAgent.isOnOffMeshLink)
{
StartCoroutine(Jump(1.0f));
return;
}
if ((!hasPath && !pathPending) || pathStatus == NavMeshPathStatus.PathInvalid )
{
SetNextDestination(true);
}
else if(isPathStale)
{
SetNextDestination(false);
}
}
IEnumerator Jump (float duration)
{
OffMeshLinkData data = _navAgent.currentOffMeshLinkData;
Vector3 startPos = _navAgent.transform.position;
Vector3 endPos = data.endPos + (_navAgent.baseOffset *Vector3.up);
float time = 0f;
while(time <= duration)
{
float t = time / duration;
_navAgent.transform.position = Vector3.Lerp(startPos, endPos, t) + (jumpCurve.Evaluate(t) *Vector3.up);
time += Time.deltaTime;
yield return null;
}
_navAgent.CompleteOffMeshLink();
}
Is there a conflict of some sort with the agent’s update position field?
Here’s an image of how it looks. The object snaps back and forth to the navmesh agent collider.
