use "stopping distance" from NavMeshAgent, don't try to manually stop him

This commit is contained in:
in0finite 2022-03-20 23:20:39 +01:00
parent b8300b25ec
commit 3124685f47
2 changed files with 21 additions and 9 deletions

View file

@ -224,12 +224,7 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
}
_ped.MovementAgent.Destination = targetPos;
Vector3? calculatedDestination = _ped.MovementAgent.CalculatedDestination;
Vector3 targetPosToCalculateDistance = ignoreCalculatedDestination ? targetPos : calculatedDestination.GetValueOrDefault(targetPos);
float distance = (_ped.transform.position - targetPosToCalculateDistance).magnitude;
if (distance <= currentStoppingDistance)
return;
_ped.MovementAgent.StoppingDistance = currentStoppingDistance;
Vector3 desiredVelocity = _ped.MovementAgent.DesiredVelocity.WithXAndZ();

View file

@ -21,14 +21,31 @@ namespace SanAndreasUnity.Utilities
private float m_lastTimeWhenWarped = 0f;
private float m_timeSinceSampledOffNavMesh = 0f;
public Vector3 DesiredVelocity => m_sampledPosOffNavMesh.HasValue
? (m_sampledPosOffNavMesh.Value - this.NavMeshAgent.transform.position).normalized
: this.NavMeshAgent.desiredVelocity;
public Vector3 DesiredVelocity
{
get
{
if (!m_sampledPosOffNavMesh.HasValue)
return this.NavMeshAgent.desiredVelocity;
Vector3 diff = m_sampledPosOffNavMesh.Value - this.NavMeshAgent.transform.position;
float distance = diff.magnitude;
if (distance <= this.StoppingDistance)
return Vector3.zero;
return diff / distance;
}
}
public Vector3? CalculatedDestination { get; private set; } = null;
private Vector3? m_sampledPosOffNavMesh = null;
public float StoppingDistance
{
get => this.NavMeshAgent.stoppingDistance;
set => this.NavMeshAgent.stoppingDistance = value;
}
void Awake()