Skip to content

Commit 3e76da7

Browse files
Sebastian LagueSebastian Lague
authored andcommitted
Added tutorial projects
0 parents  commit 3e76da7

596 files changed

Lines changed: 1408 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Episode 2 - grid/Assets/Grid.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
4+
public class Grid : MonoBehaviour {
5+
6+
public LayerMask unwalkableMask;
7+
public Vector2 gridWorldSize;
8+
public float nodeRadius;
9+
Node[,] grid;
10+
11+
float nodeDiameter;
12+
int gridSizeX, gridSizeY;
13+
14+
void Start() {
15+
nodeDiameter = nodeRadius*2;
16+
gridSizeX = Mathf.RoundToInt(gridWorldSize.x/nodeDiameter);
17+
gridSizeY = Mathf.RoundToInt(gridWorldSize.y/nodeDiameter);
18+
CreateGrid();
19+
}
20+
21+
void CreateGrid() {
22+
grid = new Node[gridSizeX,gridSizeY];
23+
Vector3 worldBottomLeft = transform.position - Vector3.right * gridWorldSize.x/2 - Vector3.forward * gridWorldSize.y/2;
24+
25+
for (int x = 0; x < gridSizeX; x ++) {
26+
for (int y = 0; y < gridSizeY; y ++) {
27+
Vector3 worldPoint = worldBottomLeft + Vector3.right * (x * nodeDiameter + nodeRadius) + Vector3.forward * (y * nodeDiameter + nodeRadius);
28+
bool walkable = !(Physics.CheckSphere(worldPoint,nodeRadius,unwalkableMask));
29+
grid[x,y] = new Node(walkable,worldPoint);
30+
}
31+
}
32+
}
33+
34+
public Node NodeFromWorldPoint(Vector3 worldPosition) {
35+
float percentX = (worldPosition.x + gridWorldSize.x/2) / gridWorldSize.x;
36+
float percentY = (worldPosition.z + gridWorldSize.y/2) / gridWorldSize.y;
37+
percentX = Mathf.Clamp01(percentX);
38+
percentY = Mathf.Clamp01(percentY);
39+
40+
int x = Mathf.RoundToInt((gridSizeX-1) * percentX);
41+
int y = Mathf.RoundToInt((gridSizeY-1) * percentY);
42+
return grid[x,y];
43+
}
44+
45+
void OnDrawGizmos() {
46+
Gizmos.DrawWireCube(transform.position,new Vector3(gridWorldSize.x,1,gridWorldSize.y));
47+
48+
49+
if (grid != null) {
50+
foreach (Node n in grid) {
51+
Gizmos.color = (n.walkable)?Color.white:Color.red;
52+
Gizmos.DrawCube(n.worldPosition, Vector3.one * (nodeDiameter-.1f));
53+
}
54+
}
55+
}
56+
}
57+
58+
public class Node {
59+
60+
public bool walkable;
61+
public Vector3 worldPosition;
62+
63+
public Node(bool _walkable, Vector3 _worldPos) {
64+
walkable = _walkable;
65+
worldPosition = _worldPos;
66+
}
67+
}

Episode 2 - grid/Assets/Grid.cs.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.

Episode 2 - grid/Assets/Ground.mat

4.94 KB
Binary file not shown.

Episode 2 - grid/Assets/Ground.mat.meta

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
4.94 KB
Binary file not shown.

Episode 2 - grid/Assets/Obstacles.mat.meta

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
24.7 KB
Binary file not shown.

Episode 2 - grid/Assets/Pathfinding Test.unity.meta

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
4.84 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4;0;196609;-1

0 commit comments

Comments
 (0)