/// <summary> /// Check if there are other walk action having the same destination. /// </summary> /// <param name="self"></param> /// <param name="targetPos"> Current targeting position. </param> /// <param name="overlapDistance"> Allow overlapping distance. </param> /// <returns> /// Return true, if it does overlaps with other walk action. /// Return false, if it does NOT overlap with other walk action. /// </returns> public JCS_3DWalkAction OverlapWithOthers(JCS_3DWalkAction self, Vector3 targetPos, float overlapDistance) { foreach (JCS_3DWalkAction wa in mWalkActions) { if (self == wa) { continue; } Vector3 dest = wa.TargetDestination; float distance = Vector3.Distance(targetPos, dest); if (distance <= overlapDistance) { return(wa); } } return(null); }
/// <summary> /// Add walk action to get manage. /// </summary> /// <param name="wa"> Target walk action you want to get manage. </param> public void AddWalkAction(JCS_3DWalkAction wa) { mWalkActions = JCS_Utility.RemoveEmptySlotIncludeMissing <JCS_3DWalkAction>(mWalkActions); mWalkActions.Add(wa); }
/// <summary> /// Target one player and do in target action. /// </summary> /// <param name="target"> Target we are following. </param> public void TargetOne(Transform target) { if (!mNavMeshAgent.enabled) { return; } if (!IsTargetType()) { return; } // if target is does not exist, end function call. if (target == null) { JCS_Debug.LogError("The transform you are targeting is null"); return; } JCS_3DWalkActionManager wam = JCS_3DWalkActionManager.instance; if (mSearchCounter == mSearchCount) { // reset search count. mSearchCounter = 0; // exit out of recursive function call... return; } // reset the path every time it request. mNavMeshAgent.ResetPath(); // calculate the distance and range relationship, and find out the // position agent are approach to. Vector3 targetPos = GetPosByWalkType(target); JCS_3DWalkAction overlapped = null; if (!mAllowOverlapDestination) { overlapped = wam.OverlapWithOthers(this, targetPos, mOverlapDistance); } // set to the destination. bool found = false; if (!overlapped) { found = mNavMeshAgent.SetDestination(targetPos); } ++mSearchCounter; #if (UNITY_EDITOR) this.mFoundPath = found; #endif // if faild, try it again. if (!found) { if (overlapped) { TargetOne(overlapped.transform); } else { TargetOne(target); } } else { this.mTargetDestination = targetPos; // if succesed, reset search count. mSearchCounter = 0; } }