check stopping distance to calculated destination, not to assigned destination

This commit is contained in:
in0finite 2022-03-20 12:36:54 +01:00
parent 08bb988a55
commit 77cc164ea4
2 changed files with 25 additions and 6 deletions

View file

@ -127,8 +127,6 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
public override void UpdateState()
{
_ped.MovementAgent.Destination = null;
if (null == this.LeaderPed)
{
_pedAI.StartIdling();
@ -175,6 +173,7 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
Vector3 targetPos = this.LeaderPed.transform.position;
float currentStoppingDistance = 3f;
bool ignoreCalculatedDestination = false;
if (this.LeaderPed.IsInVehicleSeat && !this.MyPed.IsInVehicle)
{
@ -203,6 +202,7 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
// move toward the seat
targetPos = closestfreeSeat.tr.position;
currentStoppingDistance = 0.01f;
ignoreCalculatedDestination = true;
}
}
@ -218,14 +218,19 @@ namespace SanAndreasUnity.Behaviours.Peds.AI
if (this.MyPed.IsInVehicle)
{
_ped.MovementAgent.Destination = null;
return;
float distance = (_ped.transform.position - targetPos).magnitude;
if (distance <= currentStoppingDistance)
return;
}
_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;
Vector3 desiredVelocity = _ped.MovementAgent.DesiredVelocity.WithXAndZ();
if (desiredVelocity != Vector3.zero)

View file

@ -22,6 +22,8 @@ namespace SanAndreasUnity.Utilities
public Vector3 DesiredVelocity => this.NavMeshAgent.desiredVelocity;
public Vector3? CalculatedDestination { get; private set; } = null;
void Awake()
@ -91,12 +93,17 @@ namespace SanAndreasUnity.Utilities
Debug.Log($"warped agent {this.name} - bWarp {bWarp}, isOnNavMesh {agent.isOnNavMesh}, pos diff {retreivedNextPosition - myPosition}, bSetDestination {bSetDestination}", this);
}
// no need to set velocity, it's automatically set by Agent
//this.NavMeshAgent.velocity = this.Velocity;
// update calculated destination
this.CalculatedDestination = agent.hasPath ? agent.destination : (Vector3?)null;
if (!this.Destination.HasValue)
{
m_lastAssignedDestination = null;
m_lastPositionWhenAssignedDestination = null;
this.CalculatedDestination = null;
if (agent.hasPath)
agent.ResetPath();
@ -169,6 +176,7 @@ namespace SanAndreasUnity.Utilities
m_lastTimeWhenSearchedForPath = Time.time;
m_lastAssignedDestination = this.Destination.Value;
m_lastPositionWhenAssignedDestination = navMeshAgent.transform.position;
this.CalculatedDestination = null;
// here we need to sample position on navmesh first, because otherwise agent will fail
// to calculate path if target position is not on navmesh, and as a result he will be stopped
@ -186,6 +194,12 @@ namespace SanAndreasUnity.Utilities
var navMeshPath = new NavMeshPath();
NavMesh.CalculatePath(navMeshAgent.nextPosition, hit.position, navMeshAgent.areaMask, navMeshPath);
navMeshAgent.path = navMeshPath;
this.CalculatedDestination = navMeshAgent.hasPath ? navMeshAgent.destination : (Vector3?)null;
}
else
{
// TODO: reset agent's path
}
}