Skip to content

Instantly share code, notes, and snippets.

@AG-Dan
Created January 27, 2018 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AG-Dan/0ce3d6a551a3a855df888506d3631d68 to your computer and use it in GitHub Desktop.
Save AG-Dan/0ce3d6a551a3a855df888506d3631d68 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.AI;
namespace DunGen.Dev
{
[RequireComponent(typeof(NavMeshAgent))]
public class LinearOffMeshLinkTraverser : MonoBehaviour
{
private NavMeshAgent agent;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
agent.autoTraverseOffMeshLink = false;
}
private void Update()
{
if (!agent.isOnOffMeshLink)
return;
var data = agent.currentOffMeshLinkData;
Vector3 destination = data.endPos + Vector3.up * agent.baseOffset;
agent.transform.position = Vector3.MoveTowards(agent.transform.position, destination, agent.speed * Time.deltaTime);
// Check if we're close enough to the target destination..
const float thresholdDistSqr = 0.01f * 0.01f;
float distSqr = (agent.transform.position - destination).sqrMagnitude;
// ..if so, tell the NavMeshAgent that we're done traversing this OffMeshLink
if (distSqr < thresholdDistSqr)
agent.CompleteOffMeshLink();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment