80 lines
2.5 KiB
Markdown
80 lines
2.5 KiB
Markdown
### NavMesh Overview
|
||
|
||
The **NavMesh** system is your go-to for pathfinding and AI movement in 3D environments. Imagine you're baking a map that your game characters can navigate on — that’s what the NavMesh is. You can tweak how characters move, handle obstacles, and even create custom paths.
|
||
|
||
---
|
||
|
||
### Static Properties
|
||
|
||
| Property | Description |
|
||
|---|---|
|
||
| `AllAreas` | Think of this as selecting *everywhere* your AI can walk. |
|
||
| `avoidancePredictionTime` | How far in the future Unity should predict AI avoiding each other. |
|
||
| `onPreUpdate` | A callback you can hook into right before the NavMesh updates. |
|
||
| `pathfindingIterationsPerFrame` | Controls how much pathfinding Unity does each frame. More means better accuracy but slower frames. |
|
||
|
||
---
|
||
|
||
### Key Static Methods (Chill Teacher Mode)
|
||
|
||
**1. `AddLink`**
|
||
|
||
This method is all about creating *custom* paths between two points on the NavMesh — perfect if you want shortcuts or special routes.
|
||
|
||
```csharp
|
||
NavMesh.AddLink(new NavMeshLinkData()
|
||
{
|
||
startPosition = new Vector3(0, 0, 0),
|
||
endPosition = new Vector3(10, 0, 10),
|
||
width = 2.0f
|
||
});
|
||
```
|
||
- **Use Case:** You want to build a bridge between two points that aren't directly connected.
|
||
|
||
---
|
||
|
||
**2. `CalculatePath`**
|
||
|
||
Wanna know how your AI is gonna move from Point A to Point B? Use this to calculate that path ahead of time.
|
||
|
||
```csharp
|
||
NavMeshPath path = new NavMeshPath();
|
||
NavMesh.CalculatePath(startPos, endPos, NavMesh.AllAreas, path);
|
||
```
|
||
- **Cool Trick:** You can check if the path is valid or not (`path.status`), which helps avoid bad routes.
|
||
|
||
---
|
||
|
||
**3. `FindClosestEdge`**
|
||
|
||
Ever needed to know where the closest boundary is in your NavMesh? This one’s handy for that!
|
||
|
||
```csharp
|
||
NavMeshHit hit;
|
||
if (NavMesh.FindClosestEdge(transform.position, out hit, NavMesh.AllAreas))
|
||
{
|
||
Debug.Log("Edge found at: " + hit.position);
|
||
}
|
||
```
|
||
- **Why It’s Useful:** It's like your AI has a radar for walls and limits. Use it to keep them in bounds.
|
||
|
||
---
|
||
|
||
**4. `Raycast`**
|
||
|
||
This is like drawing an invisible line to see if the AI can walk straight to a point.
|
||
|
||
```csharp
|
||
NavMeshHit hit;
|
||
if (!NavMesh.Raycast(startPos, endPos, out hit, NavMesh.AllAreas))
|
||
{
|
||
Debug.Log("Line of sight is clear!");
|
||
}
|
||
```
|
||
- **Pro Tip:** Use it to test if obstacles are in the way, so you can reroute AI.
|
||
|
||
---
|
||
|
||
These examples should give you a good starting point to experiment and make your AI navigation smarter. Keep in mind, you’ll need to bake your NavMesh in the scene first!
|
||
|
||
**Generated by ChatGPT** |