Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 *
 *	Adventure Creator
 *	by Chris Burton, 2013-2014
 *	
 *	"NavigationEngine_meshCollider.cs"
 * 
 *	This script uses Unity's built-in Navigation
 *	system to allow pathfinding in a scene.
 * 
 */

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif

public class NavigationEngine_UnityNavigation : NavigationEngine
{

	public override Vector3[] GetPointsArray (Vector3 startPosition, Vector3 targetPosition)
	{
		NavMeshHit hit = new NavMeshHit();
		NavMeshPath _path = new NavMeshPath();

		if (!NavMesh.CalculatePath (startPosition, targetPosition, -1, _path))
		{
			// Could not find path with current vectors
			float maxDistance = 0.001f;

			for (maxDistance = 0.001f; maxDistance < 1f; maxDistance += 0.05f)
			{
				if (NavMesh.SamplePosition (startPosition, out hit, maxDistance, -1))
				{
					startPosition = hit.position;
					break;
				}
			}

			for (maxDistance = 0.001f; maxDistance < 1f; maxDistance += 0.05f)
			{
				if (NavMesh.SamplePosition (targetPosition, out hit, maxDistance, -1))
				{
					targetPosition = hit.position;
					break;
				}
			}

			NavMesh.CalculatePath (startPosition, targetPosition, -1, _path);
		}

		List<Vector3> pointArray = new List<Vector3>();
		foreach (Vector3 corner in _path.corners)
		{
			pointArray.Add (corner);
		}
		if (pointArray.Count > 1 && pointArray[0].x == startPosition.x && pointArray[0].z == startPosition.z)
		{
			pointArray.RemoveAt (0);
		}
		else if (pointArray.Count == 0)
		{
			pointArray.Clear ();
			pointArray.Add (targetPosition);
		}

		return (pointArray.ToArray ());
	}


	public override string GetPrefabName ()
	{
		return ("NavMeshSegment");
	}


	public override void SetVisibility (bool visibility)
	{
		NavMeshSegment[] navMeshSegments = FindObjectsOfType (typeof (NavMeshSegment)) as NavMeshSegment[];
		
		#if UNITY_EDITOR
		Undo.RecordObjects (navMeshSegments, "NavMesh visibility");
		#endif
		
		foreach (NavMeshSegment navMeshSegment in navMeshSegments)
		{
			if (visibility)
			{
				navMeshSegment.Show ();
			}
			else
			{
				navMeshSegment.Hide ();
			}
			
			#if UNITY_EDITOR
			EditorUtility.SetDirty (navMeshSegment);
			#endif
		}
	}

}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/AdventureCreator/Scripts/Navigation/NavigationEngine_UnityNavigation.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000