when sampling target position, do multi-sampling with distance levels

This commit is contained in:
in0finite 2022-04-16 23:16:16 +02:00
parent 3da0a75562
commit 7ee9bbe425
2 changed files with 21 additions and 1 deletions

View file

@ -561,6 +561,10 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
warpSampleDistance: 4.5 warpSampleDistance: 4.5
destinationSampleDistances:
- 3
- 8
- 100
--- !u!1 &1531309216951106 --- !u!1 &1531309216951106
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View file

@ -19,6 +19,8 @@ namespace SanAndreasUnity.Utilities
public float warpSampleDistance = 4.5f; public float warpSampleDistance = 4.5f;
public float[] destinationSampleDistances = new float[] { 3f, 8f, 100f };
public Vector3 DesiredDirection public Vector3 DesiredDirection
{ {
get get
@ -293,7 +295,7 @@ namespace SanAndreasUnity.Utilities
// TODO: performance optimization: this can be done "asyncly": register pathfinding request, and process // TODO: performance optimization: this can be done "asyncly": register pathfinding request, and process
// requests from all agents in Update() function of some Manager script, with some time limit (eg. 1 ms) // requests from all agents in Update() function of some Manager script, with some time limit (eg. 1 ms)
if (NavMesh.SamplePosition(this.Destination.Value, out var hit, 100f, navMeshAgent.areaMask)) if (this.SamplePosition(this.Destination.Value, this.destinationSampleDistances, out var hit))
{ {
// TODO: re-use NavMeshPath object // TODO: re-use NavMeshPath object
var navMeshPath = new NavMeshPath(); var navMeshPath = new NavMeshPath();
@ -320,6 +322,20 @@ namespace SanAndreasUnity.Utilities
this.NavMeshAgent.ResetPath(); this.NavMeshAgent.ResetPath();
} }
private bool SamplePosition(Vector3 pos, float[] sampleDistances, out NavMeshHit hit)
{
int areaMask = this.NavMeshAgent.areaMask;
for (int i = 0; i < sampleDistances.Length; i++)
{
if (NavMesh.SamplePosition(pos, out hit, sampleDistances[i], areaMask))
return true;
}
hit = default;
return false;
}
private void OnDrawGizmosSelected() private void OnDrawGizmosSelected()
{ {
if (null == this.NavMeshAgent) if (null == this.NavMeshAgent)