9
2024
Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
Required Packages
– Tested this on Unity 2023.2.3f1 (but works in other versions too)
– AI Navigation : https://docs.unity3d.com/Packages/com.unity.ai.navigation@2.0/manual/index.html
– 2D Tilemap Editor : https://docs.unity3d.com/Packages/com.unity.2d.tilemap@1.0/manual/index.html
– 2D Tilemap Extras : https://docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@4.0/manual/index.html
Setup Tilemap with 3D Prefabs
– Add 2D Tilemap to Scene
– Set Grid Cell layout to XZY (so that its flat on ground)
– Create 3D Box in scene view, name it TileGrass, give it green color material, Make prefab from it (drag to Project window, some folder)
– Create 3D Box in scene view, name it TilePath, give it brown color material, Make prefab from it (drag to Project window, some folder)
– Project window: Create 2D/Tile palette/Rectangular
– Drag & Drop TileGrass and TilePath prefabs from Project window into Tile palette grid
– Select TileGrass in Tile Palette
– At Tile Palette window bottom left, select “GameObject Brush” from the dropdown (and click Toggle Brush Inspector button (top right bottom in Tile Palette window) *If they are not visible, resize tilepalette
– Assign Element 0: TileGrass prefab from Project window (do the same for TilePath)
– Now with TileGrass selected in Tile Palette window, you can paint into Scene Tilemap (select it first)
– Then toggle Paint mode in Tile Palette window top toolbar (the pencil brush button)
– You can erase painted tiles with left shift+left mouse button (or press I while mouse over some painted tile to Pick that tile as brush)
Adding NavMesh Pathfinding
– Select TileGrass prefab from Project window, add NavMesh Obstacle component to it
– Create new empty Gameobject in Scene, add NavMeshSurface component to it
– Test Baking, if road does not get over-layed with navmesh then open Window/AI/Navigation
– Make Agent radius 0.2 (so that it fits the road)
– Press Bake again in NavMeshSurface gameobject, blue area should appear over Path
Setup AI Agent
– Create new capsule gameobject in Scene (name it “Agent”)
– Place it at your tilemap path start position, can make it 0.5x smaller to fit better
– Add NavMeshAgent component to this gameobject
– Add this SampleAgent script to it https://gist.github.com/unitycoder/8e6e27faca3d40abf50c0842a51af558
– Create new empty gameobject in scene (name it “GoalGo”)
– Move it at the end of your Path
– Assign “GoalGo” gameobject into SampleAgent script in the gameobject
– Press Play
– Agent should move towards goal position, following the available path
References:
– NavMeshSurface: https://docs.unity3d.com/Packages/com.unity.ai.navigation@2.0/manual/NavMeshSurface.html
– Better 2D Navmesh (works with 2D objects instead of 3D objects) https://github.com/h8man/NavMeshPlus
Video:
Related Posts
Leave a comment
Recent posts
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on