Created
April 30, 2019 03:51
-
-
Save AntipodeStudiosDev/48ef97c66ac66e68a8f7dd5092b9a7d9 to your computer and use it in GitHub Desktop.
Unity3D Add Colliders To Terrain Details, Mouse Click To Remove Terrain Details
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class AddColliders : MonoBehaviour | |
{ | |
public Terrain terrain; | |
private TreeInstance[] _originalTrees; | |
void Start() | |
{ | |
terrain = GetComponent<Terrain>(); | |
_originalTrees = terrain.terrainData.treeInstances; | |
for (int i = 0; i < terrain.terrainData.treeInstances.Length; i++) | |
{ | |
TreeInstance treeInstance = terrain.terrainData.treeInstances[i]; | |
GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube); | |
box.tag = "Plant" as string; | |
BoxCollider boxCollider = box.GetComponent<BoxCollider>() as BoxCollider; | |
boxCollider.center = new Vector3(0, 30, 0); | |
DestroyablePlant plant = box.AddComponent<DestroyablePlant>(); | |
plant.terrainIndex = i; | |
box.transform.position = Vector3.Scale(treeInstance.position, terrain.terrainData.size); | |
box.transform.parent = terrain.transform; | |
box.GetComponent<MeshRenderer>().enabled = false; | |
} | |
} | |
void OnApplicationQuit() | |
{ | |
terrain.terrainData.treeInstances = _originalTrees; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using UnityEngine; | |
public class DestroyablePlant : MonoBehaviour | |
{ | |
public int terrainIndex; | |
public void Delete() | |
{ | |
Terrain terrain = Terrain.activeTerrain; | |
List<TreeInstance> plants = new List<TreeInstance>(terrain.terrainData.treeInstances); | |
plants[terrainIndex] = new TreeInstance(); | |
terrain.terrainData.treeInstances = plants.ToArray(); | |
Destroy(gameObject); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
public class DetectCollider : MonoBehaviour | |
{ | |
public DestroyablePlant destroyablePlant; | |
public GameObject cam; | |
public Text queryNameText; | |
void Update() | |
{ | |
RaycastHit queryName; | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
if (Physics.Raycast(ray, out queryName, 10)) | |
{ | |
queryNameText.text = queryName.transform.tag.ToString(); | |
if (queryNameText.text == "Plant" && Input.GetMouseButton(0)) | |
{ | |
queryName.transform.gameObject.GetComponent<DestroyablePlant>().Delete(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment