Skip to content

Commit 0bca1fa

Browse files
committed
naive implementation
0 parents  commit 0bca1fa

33 files changed

+341
-0
lines changed

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/[Ll]ibrary/
2+
/[Tt]emp/
3+
/[Oo]bj/
4+
/[Bb]uild/
5+
/[Bb]uilds/
6+
/Assets/AssetStoreTools*
7+
8+
# Autogenerated VS/MD solution and project files
9+
ExportedObj/
10+
*.csproj
11+
*.unityproj
12+
*.sln
13+
*.suo
14+
*.tmp
15+
*.user
16+
*.userprefs
17+
*.pidb
18+
*.booproj
19+
*.svd
20+
21+
22+
# Unity3D generated meta files
23+
*.pidb.meta
24+
25+
# Unity3D Generated File On Crash Reports
26+
sysinfo.txt
27+
28+
# Builds
29+
*.apk
30+
*.unitypackage

Assets/Materials.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Materials/Ground.mat

4.9 KB
Binary file not shown.

Assets/Materials/Ground.mat.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Materials/Obstacle.mat

4.9 KB
Binary file not shown.

Assets/Materials/Obstacle.mat.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scenes.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scenes/_MAIN_.unity

24.3 KB
Binary file not shown.

Assets/Scenes/_MAIN_.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Grid.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class Grid : MonoBehaviour
6+
{
7+
public Transform player;
8+
public LayerMask unwalkableMask;
9+
public Vector2 gridWorldSize;
10+
public float nodeRadius;
11+
Node[,] grid;
12+
13+
float nodeDiameter;
14+
int gridSizeX, gridSizeY;
15+
16+
void Start()
17+
{
18+
nodeDiameter = nodeRadius * 2;
19+
gridSizeX = Mathf.RoundToInt(gridWorldSize.x / nodeDiameter);
20+
gridSizeY = Mathf.RoundToInt(gridWorldSize.y / nodeDiameter);
21+
CreateGrid();
22+
}
23+
24+
void CreateGrid()
25+
{
26+
grid = new Node[gridSizeX, gridSizeY];
27+
Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.forward * gridWorldSize.y / 2;
28+
29+
for (var x = 0; x < gridSizeX; x++)
30+
{
31+
for (var y = 0; y < gridSizeY; y++)
32+
{
33+
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius);
34+
bool walkable = !(Physics.CheckSphere(worldPoint, nodeRadius, unwalkableMask));
35+
grid[x, y] = new Node(walkable, worldPoint, x, y);
36+
}
37+
}
38+
}
39+
40+
public List<Node> GetNeighbors(Node node)
41+
{
42+
List<Node> neighbors = new List<Node>();
43+
44+
for (var x = -1; x <= 1; x++)
45+
{
46+
for (var y = -1; y <= 1; y++)
47+
{
48+
if (x == 0 && y == 0) continue;
49+
50+
int checkX = node.gridX + x;
51+
int checkY = node.gridY + y;
52+
53+
if (checkX >= 0 && checkX < gridSizeX && checkY >= 0 && checkY < gridSizeY)
54+
{
55+
neighbors.Add(grid[checkX, checkY]);
56+
}
57+
}
58+
}
59+
60+
return neighbors;
61+
}
62+
63+
public Node NodeFromWorldPoint(Vector3 worldPosition)
64+
{
65+
float percentX = (worldPosition.x + gridWorldSize.x / 2) / gridWorldSize.x;
66+
float percentY = (worldPosition.z + gridWorldSize.y / 2) / gridWorldSize.y;
67+
percentX = Mathf.Clamp01(percentX);
68+
percentY = Mathf.Clamp01(percentY);
69+
70+
int x = Mathf.RoundToInt((gridSizeX - 1) * percentX);
71+
int y = Mathf.RoundToInt((gridSizeY - 1) * percentY);
72+
73+
return grid[x, y];
74+
}
75+
76+
public List<Node> path;
77+
void OnDrawGizmos()
78+
{
79+
Gizmos.DrawWireCube(transform.position, new Vector3(gridWorldSize.x, 1, gridWorldSize.y));
80+
81+
if (grid != null)
82+
{
83+
Node playerNode = NodeFromWorldPoint(player.position);
84+
85+
foreach (Node n in grid)
86+
{
87+
Gizmos.color = n.walkable ? Color.white : Color.red;
88+
89+
if (path != null && path.Contains(n))
90+
{
91+
Gizmos.color = Color.black;
92+
}
93+
94+
if (playerNode == n)
95+
{
96+
Gizmos.color = Color.cyan;
97+
}
98+
99+
Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter - 0.1f));
100+
}
101+
}
102+
}
103+
}

Assets/Scripts/Grid.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Node.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class Node
5+
{
6+
public bool walkable;
7+
public Vector3 worldPosition;
8+
public int gridX;
9+
public int gridY;
10+
11+
public int gCost;
12+
public int hCost;
13+
public Node parent;
14+
15+
public Node(bool walkable, Vector3 worldPos, int gridX, int gridY)
16+
{
17+
this.walkable = walkable;
18+
worldPosition = worldPos;
19+
this.gridX = gridX;
20+
this.gridY = gridY;
21+
}
22+
23+
public int fCost { get { return gCost + hCost; } }
24+
}

Assets/Scripts/Node.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Pathfinding.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class Pathfinding : MonoBehaviour
6+
{
7+
public Transform seeker, target;
8+
9+
Grid grid;
10+
11+
void Awake()
12+
{
13+
grid = GetComponent<Grid>();
14+
}
15+
16+
void Update()
17+
{
18+
FindPath(seeker.position, target.position);
19+
}
20+
21+
void FindPath(Vector3 startPos, Vector3 targetPos)
22+
{
23+
Node startNode = grid.NodeFromWorldPoint(startPos);
24+
Node targetNode = grid.NodeFromWorldPoint(targetPos);
25+
List<Node> openSet = new List<Node>();
26+
HashSet<Node> closedSet = new HashSet<Node>();
27+
openSet.Add(startNode);
28+
29+
while (openSet.Count > 0)
30+
{
31+
Node currentNode = openSet[0];
32+
33+
for (var i = 1; i < openSet.Count; i++)
34+
{
35+
if (openSet[i].fCost < currentNode.fCost || openSet[i].fCost == currentNode.fCost && openSet[i].hCost < currentNode.hCost)
36+
{
37+
currentNode = openSet[i];
38+
}
39+
}
40+
41+
openSet.Remove(currentNode);
42+
closedSet.Add(currentNode);
43+
44+
if (currentNode == targetNode)
45+
{
46+
RetracePath(startNode, targetNode);
47+
return;
48+
}
49+
50+
foreach (Node neighbor in grid.GetNeighbors(currentNode))
51+
{
52+
if (!neighbor.walkable || closedSet.Contains(neighbor)) continue;
53+
54+
int newMovementCostToNeighbor = currentNode.gCost + GetDistance(currentNode, neighbor);
55+
56+
if (newMovementCostToNeighbor < neighbor.gCost || !openSet.Contains(neighbor))
57+
{
58+
neighbor.gCost = newMovementCostToNeighbor;
59+
neighbor.hCost = GetDistance(neighbor, targetNode);
60+
neighbor.parent = currentNode;
61+
62+
if (!openSet.Contains(neighbor))
63+
{
64+
openSet.Add(neighbor);
65+
}
66+
}
67+
}
68+
}
69+
}
70+
71+
void RetracePath(Node startNode, Node endNode)
72+
{
73+
List<Node> path = new List<Node>();
74+
Node currentNode = endNode;
75+
76+
while (currentNode != startNode)
77+
{
78+
path.Add(currentNode);
79+
currentNode = currentNode.parent;
80+
}
81+
82+
path.Reverse();
83+
84+
grid.path = path;
85+
}
86+
87+
// equation details: ep3 @ 14.00
88+
int GetDistance(Node nodeA, Node nodeB)
89+
{
90+
int distX = Mathf.Abs(nodeA.gridX - nodeB.gridX);
91+
int distY = Mathf.Abs(nodeA.gridY - nodeB.gridY);
92+
93+
return distX > distY ? 14 * distY + 10 * (distX - distY) : 14 * distX + 10 * (distY - distX);
94+
}
95+
}

Assets/Scripts/Pathfinding.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ProjectSettings/AudioManager.asset

4.04 KB
Binary file not shown.
4.01 KB
Binary file not shown.

ProjectSettings/DynamicsManager.asset

4.18 KB
Binary file not shown.
4.01 KB
Binary file not shown.

ProjectSettings/EditorSettings.asset

4.11 KB
Binary file not shown.
4.29 KB
Binary file not shown.

ProjectSettings/InputManager.asset

5.39 KB
Binary file not shown.

ProjectSettings/NavMeshAreas.asset

4.28 KB
Binary file not shown.

ProjectSettings/NetworkManager.asset

4.02 KB
Binary file not shown.
4.26 KB
Binary file not shown.

ProjectSettings/ProjectSettings.asset

43.4 KB
Binary file not shown.

ProjectSettings/ProjectVersion.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
m_EditorVersion: 5.4.2f2-GVR11
2+
m_StandardAssetsVersion: 0

ProjectSettings/QualitySettings.asset

4.89 KB
Binary file not shown.

ProjectSettings/TagManager.asset

4.22 KB
Binary file not shown.

ProjectSettings/TimeManager.asset

4.02 KB
Binary file not shown.
4.02 KB
Binary file not shown.
4.09 KB
Binary file not shown.

0 commit comments

Comments
 (0)