├── README.md └── SetTerrainObstacles.cs /README.md: -------------------------------------------------------------------------------- 1 | # Unity-NavMesh-load navMeshObstacles for your terrain trees 2 | This is small addon for the unity NavMeshComponents that allows you to load your Terrain trees as actual obstacles into the navMeshSurface. 3 | 4 | 5 | 6 | For the Script to work you have to add Either a CapsuleCollider or a BoxCollider to your tree and ajust it on how big you wana have the NavMeshObstacle Componnent. 7 | If you don't do this, The NavMeshObstacle does not know where to get its value from and the script stops working. 8 | 9 | ![Tut1](https://user-images.githubusercontent.com/76574483/103113332-89554580-465a-11eb-9d40-5aab74c8db56.png) 10 | 11 | Check if all the trees you want to have a NavMeshObstacle for are in your Terrain Tree index. 12 | ![Tut2](https://user-images.githubusercontent.com/76574483/103113466-29ab6a00-465b-11eb-8a01-cb0c2ff31be5.png) 13 | 14 | Before hitting the play button, Check if navMEshSurface has Baked the Terrain. 15 | then add the Scrip and hit play. (it loads relativly fast on 30k trees). After Loading you should see a gameObject named "Tree_Obstacles". 16 | in there are all the NavMeshObstacle. Done. 17 | 18 | ![tut3](https://user-images.githubusercontent.com/76574483/103113717-44321300-465c-11eb-9b16-279192ba2392.png) 19 | 20 | Link to Script : https://github.com/Malikir324/Unity-NavMesh-adding-Obstacles-To-Terrain-Trees/blob/main/SetTerrainObstacles.cs 21 | -------------------------------------------------------------------------------- /SetTerrainObstacles.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.AI; 5 | 6 | public class SetTerrainObstacles : MonoBehaviour 7 | { 8 | // Start is called before the first frame update 9 | TreeInstance[] Obstacle; 10 | Terrain terrain; 11 | float width; 12 | float lenght; 13 | float hight; 14 | bool isError; 15 | void Start() 16 | { 17 | terrain = Terrain.activeTerrain; 18 | Obstacle = terrain.terrainData.treeInstances; 19 | 20 | lenght = terrain.terrainData.size.z; 21 | width = terrain.terrainData.size.x; 22 | hight = terrain.terrainData.size.y; 23 | Debug.Log("Terrain Size is :" + width + " , " + hight + " , " + lenght); 24 | 25 | int i = 0; 26 | GameObject parent = new GameObject("Tree_Obstacles"); 27 | 28 | Debug.Log("Adding "+Obstacle.Length+" navMeshObstacle Components for Trees"); 29 | foreach (TreeInstance tree in Obstacle) 30 | { 31 | Vector3 tempPos = new Vector3(tree.position.x * width, tree.position.y * hight, tree.position.z * lenght); 32 | Quaternion tempRot = Quaternion.AngleAxis(tree.rotation * Mathf.Rad2Deg, Vector3.up); 33 | 34 | GameObject obs = new GameObject("Obstacle" + i); 35 | obs.transform.SetParent(parent.transform); 36 | obs.transform.position = tempPos; 37 | obs.transform.rotation = tempRot; 38 | 39 | obs.AddComponent(); 40 | NavMeshObstacle obsElement = obs.GetComponent(); 41 | obsElement.carving = true; 42 | obsElement.carveOnlyStationary = true; 43 | 44 | if (terrain.terrainData.treePrototypes[tree.prototypeIndex].prefab.GetComponent() == null) 45 | { 46 | isError = true; 47 | Debug.LogError("ERROR There is no CapsuleCollider or BoxCollider attached to ''" + terrain.terrainData.treePrototypes[tree.prototypeIndex].prefab.name + "'' please add one of them."); 48 | break; 49 | } 50 | Collider coll = terrain.terrainData.treePrototypes[tree.prototypeIndex].prefab.GetComponent(); 51 | if(coll.GetType() == typeof(CapsuleCollider) || coll.GetType() == typeof(BoxCollider)) 52 | { 53 | 54 | if (coll.GetType() == typeof(CapsuleCollider)) 55 | { 56 | CapsuleCollider capsuleColl = terrain.terrainData.treePrototypes[tree.prototypeIndex].prefab.GetComponent(); 57 | obsElement.shape = NavMeshObstacleShape.Capsule; 58 | obsElement.center = capsuleColl.center; 59 | obsElement.radius = capsuleColl.radius; 60 | obsElement.height = capsuleColl.height; 61 | 62 | } 63 | else if (coll.GetType() == typeof(BoxCollider)) 64 | { 65 | BoxCollider boxColl = terrain.terrainData.treePrototypes[tree.prototypeIndex].prefab.GetComponent(); 66 | obsElement.shape = NavMeshObstacleShape.Box; 67 | obsElement.center = boxColl.center; 68 | obsElement.size = boxColl.size; 69 | } 70 | 71 | } 72 | else 73 | { 74 | isError = true; 75 | Debug.LogError("ERROR There is no CapsuleCollider or BoxCollider attached to ''" + terrain.terrainData.treePrototypes[tree.prototypeIndex].prefab.name + "'' please add one of them."); 76 | break; 77 | } 78 | 79 | 80 | i++; 81 | } 82 | parent.transform.position = terrain.GetPosition(); 83 | if(!isError) Debug.Log("All " + Obstacle.Length + " NavMeshObstacles were succesfully added to your Scene, Horray !"); 84 | } 85 | } 86 | --------------------------------------------------------------------------------