public void NewPath() { GameObject pathMesh = new GameObject(); pathMesh.name = "Path"; pathMesh.AddComponent(typeof(MeshFilter)); pathMesh.AddComponent(typeof(MeshRenderer)); pathMesh.AddComponent <AttachedPathScript>(); AttachedPathScript APS = (AttachedPathScript)pathMesh.GetComponent("AttachedPathScript"); APS.pathMesh = pathMesh; APS.parentTerrain = gameObject.name; APS.NewPath(); }
public void visualizePath() { GameObject pathMesh = new GameObject(); pathMesh.name = "Path"; //pathMesh.tag = "Road"; pathMesh.AddComponent(typeof(MeshFilter)); pathMesh.AddComponent(typeof(MeshRenderer)); pathMesh.AddComponent("AttachedPathScript"); AttachedPathScript APS = (AttachedPathScript)pathMesh.GetComponent("AttachedPathScript"); APS.pathMesh = pathMesh; APS.parentTerrain = gameObject; //APS.NewPath(); APS.NewPath(); APS.pathWidth = 3; //APS.pathTexture = 1; APS.isRoad = true; APS.pathSmooth = 5; //APS.pathUniform = true; //APS.pathWear = 0.5f; bool check = false; // foreach(Node node in path) { for (int i = 0; i < path.Count; i++) { if (i % 3 == 0 || i == path.Count - 1) { TerrainPathCell pathNodeCell = new TerrainPathCell(); pathNodeCell.position.x = Mathf.RoundToInt((float)((((Node)path[i]).getPosition().x / Terrain.activeTerrain.terrainData.size.x) * Terrain.activeTerrain.terrainData.heightmapResolution)); pathNodeCell.position.y = Mathf.RoundToInt((float)((((Node)path[i]).getPosition().z / Terrain.activeTerrain.terrainData.size.z) * Terrain.activeTerrain.terrainData.heightmapResolution)); pathNodeCell.heightAtCell = (Terrain.activeTerrain.SampleHeight(new Vector3(pathNodeCell.position.x, pathNodeCell.position.y))) / Terrain.activeTerrain.terrainData.size.y; //Debug.Log(pathNodeCell.heightAtCell); //Debug.Log("path node " + pathNodeCell.position); if (!APS.CreatePathNode(pathNodeCell)) { check = true; break; } if (check) { DestroyImmediate(pathMesh); continue; } } } APS.terrainCells = new TerrainPathCell[APS.terData.heightmapResolution * APS.terData.heightmapResolution]; APS.terrainCells = terrainCells; APS.FinalizePath(); APS.pathMesh.renderer.enabled = true; APS.pathMesh.renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); APS.pathMesh.renderer.sharedMaterial.color = Color.grey; //pathMesh.GetComponent<MeshCollider>().convex = true; pathMesh.AddComponent <Rigidbody>() /*.inertiaTensor = Vector3.one*/; pathMesh.GetComponent <Rigidbody>().useGravity = false; pathMesh.transform.Translate(new Vector3(0, 0.2f, 0)); pathMesh.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeAll; pathMesh.AddComponent <CollisionMover>(); for (int i = 0; i < APS.nodeObjects.Length; i++) { GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.renderer.sharedMaterial = new Material(Shader.Find("Diffuse")); sphere.renderer.sharedMaterial.color = Color.blue; sphere.transform.localScale = new Vector3(5, 5, 5); sphere.transform.position = APS.nodeObjects[i].position; sphere.AddComponent <DestroyOnLoad>(); DestroyImmediate(sphere.GetComponent <SphereCollider>()); sphere.transform.parent = pathMesh.transform; } pathMesh.GetComponent <CollisionMover>().check(); }