Question by
zackyang421 · Nov 06, 2021 at 09:54 PM ·
ainavmeshnavmeshagentpathfindingnavigation
Farthest Reachable Destination
Hello Guys:
I'm new to Unity's navigation system, and I'm building a game that the enemies would try to flee from the player if their HP reached some thresholds.
And currently, the scripts I used to define the flees are :
Vector3 Source = DamageMessage.damageSource;
Vector3 Direction = gameObject.transform.position - Source;
FleeDestination = gameObject.transform.position + Direction.normalized * 15;
FleeDestination.y = 0;
m_NavMeshAgent.SetDestination(FleeDestination);
The enemy would run for a fixed distance in the direction that opposite to the damage source, but sometimes this would result in a destination outside the Navmesh area, and the Navmesh agent would just infinitely run to the destination in a corner of the Navmesh area.
So I'm wondering if there is a way that I can set the destination to the farthest reachable location within the Navmesh area in a certain direction and distance? Any ideas?
Comment