How can I set agent's speed on off mesh link? I want it to look like a jump, but now it looks like it was in slow motion.
2 Answers
\$\begingroup\$
\$\endgroup\$
You can have an Update or FixedUpdate function check when the agent is on an off-mesh link, and modify its speed in response, as shown here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class LinkSpeedScript : MonoBehaviour
{
public NavMeshAgent agent;
public bool linking;
public float origSpeed;
// just change linkspeed to alter off mesh link traverse speed;
public float linkSpeed;
void Start()
{
origSpeed = agent.speed;
linking = false;
}
void FixedUpdate()
{
if (agent.isOnOffMeshLink && linking == false)
{
linking = true;
agent.speed = agent.speed * linkSpeed;
}
else if (agent.isOnNavMesh && linking == true)
{
linking = false;
agent.velocity = Vector3.zero;
agent.speed = origSpeed;
}
}
}
This seems to work with my NavMeshAgent settings.
\$\begingroup\$
\$\endgroup\$
This unity forum post suggests handling the traversing yourself.
if(agent.isOnOffMeshLink && !MoveAcrossNavMeshesStarted){ StartCoRoutine(MoveAcrossNavMeshLink()); MoveAcrossNavMeshesStarted=true; } IEnumerator MoveAcrossNavMeshLink() { OffMeshLinkData data = agent.currentOffMeshLinkData; agent.updateRotation = false; Vector3 startPos = agent.transform.position; Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset; float duration = (endPos-startPos).magnitude/agent.Velocity.magnitude; float t = 0.0f; float tStep = 1.0f/duration; while(t<1.0f){ transform.position = Vector3.Lerp(startPos,endPos,t); agent.destination = transform.position; t+=tStep*Time.deltaTime; yield return null; } transform.position = endPos; agent.updateRotation = true; agent.CompleteOffMeshLink(); MoveAcrossNavMeshesStarted= false; }
To make it look like a jump, you could
- lerp between the two positions and play a jump animation on a child node of the hierarchy
- calculate the y-position yourself, e.g.
s = 0,5 · a · t2 + vo · t + s0
withvobeing the initial velocity ands0being the inital position. Use this formula for y.