本文整理汇总了C#中UnityEngine.NavMeshPath类的典型用法代码示例。如果您正苦于以下问题:C# NavMeshPath类的具体用法?C# NavMeshPath怎么用?C# NavMeshPath使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NavMeshPath类属于UnityEngine命名空间,在下文中一共展示了NavMeshPath类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CalculatePathLength
float CalculatePathLength(Vector3 targetPosition)
{
// Create a path and set it based on a target position.
NavMeshPath 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.
Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];
// The first point is the enemy's position.
allWayPoints[0] = transform.position;
// The last point is the target position.
allWayPoints[allWayPoints.Length - 1] = targetPosition;
// The points inbetween are the corners of the path.
for (int 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;
}
示例2: InteractWithNearestState
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;
}
示例3: DoCalculatePath
void DoCalculatePath()
{
_getNavMeshPathProxy();
if (_NavMeshPathProxy ==null)
{
return;
}
NavMeshPath _path = new NavMeshPath();
bool _found = NavMesh.CalculatePath(sourcePosition.Value,targetPosition.Value,passableMask.Value,_path);
_NavMeshPathProxy.path = _path;
resultingPathFound.Value = _found;
if (_found)
{
if ( ! FsmEvent.IsNullOrEmpty(resultingPathFoundEvent) ){
Fsm.Event(resultingPathFoundEvent);
}
}else
{
if (! FsmEvent.IsNullOrEmpty(resultingPathNotFoundEvent) ){
Fsm.Event(resultingPathNotFoundEvent);
}
}
}
示例4: OnEnter
public override void OnEnter(ref AiParam _param)
{
if (_param == null || _param.NavAgent == null)
return;
_param.OnAiActionChanged (UnityChan_Ctrl.ActionState.Walk);
//_param.NavAgent.Resume ();
Movement (ref _param);
int searchMax = 30;
for (var i = 0; i < searchMax; i++)
{
Vector3 randomPoint = _param.Owner.transform.position + Random.insideUnitSphere * 3F;
NavMeshHit hit;
if (NavMesh.SamplePosition (randomPoint, out hit, 1F, 0xFF)) {
NavMeshPath path = new NavMeshPath();
if(_param.NavAgent.CalculatePath(hit.position, path) == true)
{
_param.NavAgent.SetPath(path);
return;
}
}
}
}
示例5: DrawPath
void DrawPath(NavMeshPath path) {
if (path.corners.Length < 2)
return;
switch (path.status) {
case NavMeshPathStatus.PathComplete:
c = Color.white;
break;
case NavMeshPathStatus.PathInvalid:
c = Color.red;
break;
case NavMeshPathStatus.PathPartial:
c = Color.yellow;
break;
}
Vector3 previousCorner = path.corners[0];
int i = 1;
while (i < path.corners.Length) {
Vector3 currentCorner = path.corners[i];
Debug.DrawLine(previousCorner, currentCorner, c);
previousCorner = currentCorner;
i++;
}
}
示例6: Update
void Update()
{
/*if (oldpos != transform.position) {
oldpos = newpos;
newpos = transform.position;
} else {
anim.SetBool("isWalking", false);
}*/
if (Input.touchCount == 1)
{
Vector3 moveTo = GetPositionTouch();
//player.SetDestination(moveTo);
//anim.SetBool("isWalking", true);
NavMeshPath path = new NavMeshPath();
GetComponent<NavMeshAgent>().CalculatePath(moveTo, path);
//Debug.Log(path.status);
if (path.status == NavMeshPathStatus.PathComplete)
{
TouchIndicator(moveTo);
player.SetDestination(moveTo);
anim.SetBool("isWalking", true);
}
}
//Debug.Log (player.remainingDistance);
if (player.remainingDistance == 0) {
anim.SetBool("isWalking", false);
}
}
示例7: Start
// Use this for initialization
void Start()
{
destination = transform.position;
path = new NavMeshPath();
CalcPath();
rigidBody = GetComponent<Rigidbody>();
}
示例8: CalculatePathLength
float CalculatePathLength(Vector3 targetPosition)
{
//To calculate how far the sound can travel - identifies player behind corners
NavMeshPath path = new NavMeshPath();
if (nav.enabled)
nav.CalculatePath(targetPosition, path);
Vector3[] allWayPoints = new Vector3[path.corners.Length + 2]; //+2 to allow for the enemy and player positions
allWayPoints[0] = transform.position;
allWayPoints[allWayPoints.Length - 1] = targetPosition;
for (int i = 0; i < path.corners.Length; i++)
{
allWayPoints[i + 1] = path.corners[i];
}
float pathLength = 0f;
for (int i = 0; i < allWayPoints.Length-1; i++)
{
pathLength += Vector3.Distance(allWayPoints[i], allWayPoints[i + 1]);
}
return pathLength;
}
示例9: CalculatePathLength
// this is used for finding how long a sound has to travel
float CalculatePathLength(Vector3 targetPosition)
{
NavMeshPath path = new NavMeshPath();
if (nav.enabled)
nav.CalculatePath(targetPosition, path);
Vector3[] allWayPoints = new Vector3[path.corners.Length + 2];
//first point in array should be the enemy's posiotn
allWayPoints[0] = transform.position;
allWayPoints[allWayPoints.Length - 1] = targetPosition;
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;
}
示例10: DrawLinesWithNavigation
//draw lines using navmesh
void DrawLinesWithNavigation()
{
if(useOwnNavSystem == false)
{
int testId2 = 0;
int testIdPlus2 = 1;
List<Vector3> waypointVector3 = new List<Vector3>();
while(testId2 < waypointList.Length)
{
if(testIdPlus2 >= waypointList.Length)
{
testIdPlus2 = 0;
}
NavMeshPath pathMain = new NavMeshPath();
NavMesh.CalculatePath(waypointList[testId2].transform.position, waypointList[testIdPlus2].transform.position, -1, pathMain);
int testId3 = 0;
while(testId3 < pathMain.corners.Length)
{
waypointVector3.Add(pathMain.corners[testId3]);
testId3 += 1;
}
testId2 += 1;
testIdPlus2 += 1;
}
//draw the lines
int testId = 0;
int testIdPlus = 1;
while(testId < waypointVector3.Count)
{
if(testIdPlus >= waypointVector3.Count)
{
testIdPlus = 0;
}
Gizmos.color = Color.magenta;
Gizmos.DrawLine(waypointVector3[testId], waypointVector3[testIdPlus]);
testId += 1;
testIdPlus += 1;
}
}
}
示例11: FindNearest
//returns the nearest portal of a container, based on the position
static Vector3? FindNearest(Transform parent, Vector3 pos)
{
//initialize variables
NavMeshPath path = new NavMeshPath();
Vector3? nearest = null;
float distance = Mathf.Infinity;
//don't continue if the parent does not exist
if (!portals.ContainsKey(parent)) return null;
//loop over portals of this portal container,
//find the shortest NavMesh path to a portal
for (int i = 0; i < portals[parent].Count; i++)
{
Vector3 portal = portals[parent][i].position;
float length = Mathf.Infinity;
//let Unity calculate the path and set length, if valid
if (NavMesh.CalculatePath(pos, portal, -1, path)
&& path.status == NavMeshPathStatus.PathComplete)
length = PathLength(path);
if (length < distance)
{
distance = length;
nearest = portal;
}
}
return nearest;
}
示例12: Start
void Start()
{
anim = GetComponent<Animator>();
anim.SetBool("isWalking", true);
NavMeshPath path = new NavMeshPath();
agent.destination = target.position;
}
示例13: Awake
public virtual void Awake()
{
nav = GetComponent<NavMeshAgent> ();
vision = GetComponent<Vision> ();
player = GameObject.FindGameObjectWithTag ("Player").transform;
path = new NavMeshPath ();
}
示例14: findNextDestination
public void findNextDestination()
{
NavMeshPath path = new NavMeshPath();
int indexTargetChosen = -1;
bool found = false;
int count = 0;
if (getNumberOfTargetsOff() > 0)
{
while (!found && count < 99)
{
indexTargetChosen = Random.Range(0, targets.Length);
if (!targets[indexTargetChosen].isOn)
{
m_Agent.CalculatePath(targets[indexTargetChosen].transform.position, path);
if (path.status == NavMeshPathStatus.PathComplete)
{
found = true;
}
}
count++;
}
}
if (found)
{
Vector3 destination = targets[indexTargetChosen].transform.position;
m_Agent.destination = new Vector3(destination.x, transform.position.y, destination.z);
}
}
示例15: Update
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitinfo;
// LayerMask mask = 1 << LayerMask.NameToLayer("GroundLayer");
bool isCollider = Physics.Raycast(ray, out hitinfo);
if (isCollider && hitinfo.collider.tag == Tags.player)
{
cur_Role = hitinfo.collider.transform.parent.GetComponent<DBaseFightRole>();
}
if (isCollider && hitinfo.collider.tag == Tags.ground && cur_Role != null)
{
NavMeshPath path = new NavMeshPath();
cur_Role.agent.enabled = true;
cur_Role.agent.CalculatePath(hitinfo.point, path);
if (path.corners.Length >= 2)
{
cur_Role.gotoDestination(hitinfo.point);
}
cur_Role = null;
}
}
}