Skip to content

Commit

Permalink
refactor(AI): simplify 3DWalkAction's logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs090218 committed Aug 19, 2022
1 parent 9bba2cf commit 40336b3
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions Assets/JCSUnity/Scripts/Actions/3D/AI/JCS_3DWalkAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace JCSUnity
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(NavMeshObstacle))]
[RequireComponent(typeof(JCS_AdjustTimeTrigger))]
public class JCS_3DWalkAction : MonoBehaviour , JCS_IAction
public class JCS_3DWalkAction : MonoBehaviour, JCS_IAction
{
/* Variables */

Expand Down Expand Up @@ -241,14 +241,21 @@ public void TargetOne(Transform target)
// position agent are approach to.
Vector3 targetPos = GetPosByWalkType(target);

JCS_3DWalkAction overlapped = null;
if (!mAllowOverlapDestination)
overlapped = wam.OverlapWithOthers(this, targetPos, mOverlapDistance);
// try avoid invalid AABB error
bool validPos = !JCS_Mathf.IsNaN(targetPos);

// set to the destination.
JCS_3DWalkAction overlapped = null;
bool found = false;
if (!overlapped)
found = mNavMeshAgent.SetDestination(targetPos);

if (validPos)
{
if (!mAllowOverlapDestination)
overlapped = wam.OverlapWithOthers(this, targetPos, mOverlapDistance);

// set to the destination.
if (!overlapped)
found = mNavMeshAgent.SetDestination(targetPos);
}

++mSearchCounter;

Expand Down Expand Up @@ -378,32 +385,26 @@ private Vector3 CalculateClosest(Vector3 targetPos, float distance)

vec = vec.normalized;

float hyp = JCS_Mathf.PythagoreanTheorem(vec.x, vec.z, JCS_Mathf.TriSides.hyp);

float ratio = distance / hyp;

newTargetPos.x += vec.x * ratio;
newTargetPos.z += vec.z * ratio;
newTargetPos.x += vec.x * distance;
newTargetPos.z += vec.z * distance;
newTargetPos.y = this.transform.position.y;

return newTargetPos;
}

/// <summary>
/// Calculate the range and position relationship
/// in order to find the best destination in the
/// navigation map.
/// Calculate the range and position relationship in order to find
/// the best destination in the navigation map.
///
/// IMPORTANT(JenChieh): if the vector does not in the range,
/// enemy will stay at the place they are, which mean enemy
/// will do nothing...
/// IMPORTANT(jenchieh): if the vector does not in the range, enemy
/// will stay at the place they are, which mean enemy will do nothing...
/// </summary>
/// <returns> result destination </returns>
private Vector3 CalculateRange(Vector3 targetPos, float distance)
{
Vector3 newTargetPos = targetPos;

Vector3 randVec = GetRandomVec();
Vector3 randVec = GetRandomVec(); // this mean, random degree

float magnitude = distance;

Expand Down

0 comments on commit 40336b3

Please sign in to comment.