Advertisement
Guest User

Untitled

a guest
Feb 1st, 2024
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  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 worldPosition = Vector3.Scale(tree.position, terrain.terrainData.size) + terrain.transform.position;
  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 = worldPosition; // Use the adjusted world position
  37.             obs.transform.rotation = tempRot;
  38.  
  39.             obs.AddComponent<NavMeshObstacle>();
  40.             NavMeshObstacle obsElement = obs.GetComponent<NavMeshObstacle>();
  41.             obsElement.carving = true;
  42.             obsElement.carveOnlyStationary = true;
  43.  
  44.             if (terrain.terrainData.treePrototypes[tree.prototypeIndex].prefab.GetComponent<Collider>() == 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<Collider>();
  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<CapsuleCollider>();
  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<BoxCollider>();
  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.         if (!isError) Debug.Log("All " + Obstacle.Length + " NavMeshObstacles were succesfully added to your Scene, Horray !");
  83.     }
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement