Created
January 27, 2018 15:16
-
-
Save AG-Dan/0ce3d6a551a3a855df888506d3631d68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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