- Home /
Question by
JonK · Aug 30, 2013 at 02:49 AM ·
aijumpnavmeshnavmeshagenttrajectory
NavMeshAgent Calculate Jump Trajectory
I'm trying to get a NavMeshAgent to jump onto a platform or across a gap. Rather than having the agent jump in the direction of the end position I would like to have him follow a trajectory so he's guaranteed to never miss the jump.
Any ideas/suggestions?
Comment
Answer by JonK · Sep 06, 2013 at 03:04 AM
So I found away to get the enemy on to platforms but only if the platform is not hovering over the platform he is currently standing on. Thoughts?
public void Jump()
{
//at a mesh link
if(NMA.isOnOffMeshLink)
{
//Debug.Log ("begin traversing");
//not already crossing a mesh link
if (!traversingLink)
{
//stop moving (no deceleration)
NMA.Stop(true);
rigidbody.isKinematic = false;
curLink = NMA.currentOffMeshLinkData;
OffMeshLinkType OMLT = curLink.linkType;
if(OMLT == OffMeshLinkType.LinkTypeManual)
{
jumpDistance = (curLink.endPos.z - curLink.startPos.z) * 0.5f;
}
else
{
jumpDistance = (curLink.endPos.z - curLink.startPos.z) * 0.2f;
}
// Debug.Log(OMLT.ToString());
traversingLink = true;
startTime = Time.time;
}
float distCovered = (Time.time - startTime) * NMA.speed;
float journeyLength = Vector3.Distance(curLink.startPos, curLink.endPos);
float fracJourney = distCovered / journeyLength;
Vector3 EndPos = new Vector3(curLink.endPos.x, curLink.endPos.y, curLink.endPos.z + jumpDistance);
float platformHeight = EndPos.y - curLink.startPos.y;
//character should only jump upwards not be forced down
if(platformHeight < 0)
platformHeight = 0;
//move to other side of link
Vector3 newPos = Vector3.Lerp(curLink.startPos, EndPos, fracJourney);
//add hop
newPos.y += (jumpHeight + platformHeight) * Mathf.Sin(Mathf.PI * fracJourney);
transform.position = newPos;
if(Vector3.Distance(transform.position, EndPos) < 1)
{
NMA.CompleteOffMeshLink();
rigidbody.isKinematic = true;
traversingLink = false;
NMA.Resume();
}
}
}