mirror of
https://github.com/GTA-ASM/SanAndreasUnity
synced 2024-11-10 06:34:16 +00:00
when off nav mesh, update desired velocity every frame based on destination, if there is no sampled position
This commit is contained in:
parent
3124685f47
commit
565681cd75
1 changed files with 20 additions and 6 deletions
|
@ -25,10 +25,12 @@ namespace SanAndreasUnity.Utilities
|
|||
{
|
||||
get
|
||||
{
|
||||
if (!m_sampledPosOffNavMesh.HasValue)
|
||||
if (!m_destinationPosOffNavMesh.HasValue)
|
||||
return this.NavMeshAgent.desiredVelocity;
|
||||
|
||||
Vector3 diff = m_sampledPosOffNavMesh.Value - this.NavMeshAgent.transform.position;
|
||||
// TODO: if we are in range of destination, don't move
|
||||
|
||||
Vector3 diff = m_destinationPosOffNavMesh.Value - this.NavMeshAgent.transform.position;
|
||||
float distance = diff.magnitude;
|
||||
if (distance <= this.StoppingDistance)
|
||||
return Vector3.zero;
|
||||
|
@ -38,7 +40,8 @@ namespace SanAndreasUnity.Utilities
|
|||
|
||||
public Vector3? CalculatedDestination { get; private set; } = null;
|
||||
|
||||
private Vector3? m_sampledPosOffNavMesh = null;
|
||||
private Vector3? m_destinationPosOffNavMesh = null;
|
||||
private bool m_hasSampledPosOffNavMesh = false;
|
||||
|
||||
public float StoppingDistance
|
||||
{
|
||||
|
@ -136,18 +139,29 @@ namespace SanAndreasUnity.Utilities
|
|||
// try to get back to nav mesh
|
||||
m_timeSinceSampledOffNavMesh = 0f;
|
||||
|
||||
m_sampledPosOffNavMesh = this.Destination; // if position can not be sampled, go straight to destination
|
||||
m_hasSampledPosOffNavMesh = false;
|
||||
m_destinationPosOffNavMesh = this.Destination; // if position can not be sampled, go straight to destination
|
||||
|
||||
if (NavMesh.SamplePosition(myPosition, out var hit, 150f, agent.areaMask))
|
||||
m_sampledPosOffNavMesh = hit.position;
|
||||
{
|
||||
m_destinationPosOffNavMesh = hit.position;
|
||||
m_hasSampledPosOffNavMesh = true;
|
||||
}
|
||||
|
||||
//Debug.Log($"Tried to sample position off nav mesh - agent {agent.name}, sampled pos {m_sampledPosOffNavMesh}, distance {hit.distance}", this);
|
||||
}
|
||||
else
|
||||
{
|
||||
// if we are moving toward destination, not sampled position, then update position
|
||||
if (!m_hasSampledPosOffNavMesh)
|
||||
m_destinationPosOffNavMesh = this.Destination;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_timeSinceSampledOffNavMesh = 0f;
|
||||
m_sampledPosOffNavMesh = null;
|
||||
m_hasSampledPosOffNavMesh = false;
|
||||
m_destinationPosOffNavMesh = null;
|
||||
}
|
||||
|
||||
if (!this.Destination.HasValue)
|
||||
|
|
Loading…
Reference in a new issue