// Unity's example function.. public float CalculatePathLength(NavMeshAgent nav, Vector3 targetPosition) { // Added condition since it was returning impossible values if given same position :P if (nav.transform.position == targetPosition) return 0; // Create a path and set it based on a target position. var path = new NavMeshPath(); if (nav.enabled) nav.CalculatePath(targetPosition, path); // Create an array of points which is the length of the number of corners in the path + 2. var allWayPoints = new Vector3[path.corners.Length + 2]; // The first point is the player position. allWayPoints[0] = player.transform.position; // The last point is the enemy position allWayPoints[allWayPoints.Length - 1] = targetPosition; // The points inbetween are the corners of the path. for (var i = 0; i < path.corners.Length; i++) allWayPoints[i + 1] = path.corners[i]; // Create a float to store the path length that is by default 0. float pathLength = 0; // Increment the path length by an amount equal to the distance between each waypoint and the next. for (int i = 0; i < allWayPoints.Length - 1; i++) { pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]); } return pathLength; }
public InteractWithNearestState(NavMeshAgent agent, string tag, GameObject pickup) { _agent = agent; cam = _agent.gameObject.GetComponent<CharacterAnimMovement>(); _interactGameObject = pickup; var interactables = GameObject.FindGameObjectsWithTag(tag); if (interactables.Length < 1) { GameObject.FindGameObjectWithTag("AudioManager").GetComponent<AudioManager>().FemaleNoSoundPlay(); _state = State.Done; return; } foreach (var i in interactables) { _interactGameObject = i; var dest = i.GetComponent<IInteractable>(); if(!dest.CanThisBeInteractedWith(pickup)) continue; var path = new NavMeshPath(); agent.CalculatePath(dest.InteractPosition(_agent.transform.position), path); if (path.status != NavMeshPathStatus.PathComplete) continue; _intaractableGoal = dest; break; } if(_intaractableGoal == null) _state = State.Done; }
/// <summary> /// 是否可以通过导航找到路径 /// </summary> /// <param name="targetVector3"></param> /// <returns></returns> public bool IsMoveNavAgent(Vector3 targetVector3) { if (NavAgent == null) { return(false); } return(NavAgent.CalculatePath(targetVector3, NavAgent.path)); }
void Start() { agent = GetComponent<NavMeshAgent>(); NavMeshPath path = new NavMeshPath(); agent.CalculatePath(target.position, path); if (path.status == NavMeshPathStatus.PathPartial) { } }
static public int CalculatePath(IntPtr l) { try { UnityEngine.NavMeshAgent self = (UnityEngine.NavMeshAgent)checkSelf(l); UnityEngine.Vector3 a1; checkType(l, 2, out a1); UnityEngine.NavMeshPath a2; checkType(l, 3, out a2); var ret = self.CalculatePath(a1, a2); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int CalculatePath(IntPtr l) { try{ UnityEngine.NavMeshAgent self = (UnityEngine.NavMeshAgent)checkSelf(l); UnityEngine.Vector3 a1; checkType(l, 2, out a1); UnityEngine.NavMeshPath a2; checkType(l, 3, out a2); System.Boolean ret = self.CalculatePath(a1, a2); pushValue(l, ret); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static int CalculatePath(IntPtr L) { try { ToLua.CheckArgsCount(L, 3); UnityEngine.NavMeshAgent obj = (UnityEngine.NavMeshAgent)ToLua.CheckObject(L, 1, typeof(UnityEngine.NavMeshAgent)); UnityEngine.Vector3 arg0 = ToLua.ToVector3(L, 2); UnityEngine.NavMeshPath arg1 = (UnityEngine.NavMeshPath)ToLua.CheckObject(L, 3, typeof(UnityEngine.NavMeshPath)); bool o = obj.CalculatePath(arg0, arg1); LuaDLL.lua_pushboolean(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
// Berechnet die Distanz zwischen 2 Vektoren auf einem NavMesh public static float CalcPathDistance(Vector3 originPos, Vector3 targetPos, ref NavMeshAgent nav) { NavMeshPath path = new NavMeshPath(); if (nav.enabled) nav.CalculatePath(targetPos, path); Vector3[] allWayPoints = new Vector3[path.corners.Length + 2]; allWayPoints[0] = originPos; allWayPoints[allWayPoints.Length - 1] = targetPos; for (int i = 0; i < path.corners.Length; i++) { allWayPoints[i + 1] = path.corners[i]; } float pathLength = 0; for (int i = 0; i < allWayPoints.Length - 1; i++) { pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]); } return pathLength; }
public EndGameState(NavMeshAgent agent) { _agent = agent; var endPoints = GameObject.FindGameObjectWithTag(Constants.Tags.GameMaster). GetComponent<EndPoints>().Endpoints; if (endPoints.Length < 1) Debug.LogError("No interactables in scene, F**K!"); foreach (var i in endPoints) { var path = new NavMeshPath(); agent.CalculatePath(i.transform.position, path); if (path.status != NavMeshPathStatus.PathComplete) continue; _destination = i; break; } if (_destination == null) _state = State.Done; }