赞
踩
Vector3.Scale(currentTree.position, terrain.size) + Terrain.activeTerrain.transform.position;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
-
-
- public class TreeChop : MonoBehaviour
- {
- public GameObject FallingTreePrefab;
- private List<TreeInstance> TreeInstances;
- // Use this for initialization
- private void Start()
- {
- TreeInstances = new List<TreeInstance>(Terrain.activeTerrain.terrainData.treeInstances);
- Debug.Log("Tree Instances:" + TreeInstances.Count);
- }
-
- // Update is called once per frame
- private void Update()
- {
- // did we click on a tree?
- if (Input.GetMouseButtonDown(0))
- {
- DateTime start = DateTime.Now;
- RaycastHit hit;
- // This ray will see where we clicked er chopped
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
-
- // Did we hit anything at that point, out as far as 10 units?
- if (Physics.Raycast(ray, out hit, 10.0f))
- {
- // Did we even click er chop on the terrain/tree?
- if (hit.collider.name != Terrain.activeTerrain.name)
- {
- // No, must have been a frantic attack on a giant spider >:)
- return;
- }
-
- // We hit the "terrain"! Now, how high is the ground at that point?
- float sampleHeight = Terrain.activeTerrain.SampleHeight(hit.point);
-
- // If the height of the exact point we clicked/chopped at or below ground level, all we did
- // was chop dirt.
- if (hit.point.y <= sampleHeight + 0.01f)
- {
- return;
- }
-
- TerrainData terrain = Terrain.activeTerrain.terrainData;
- TreeInstance[] treeInstances = terrain.treeInstances;
-
- // Our current closest tree initializes to far away
- float maxDistance = float.MaxValue;
- // Track our closest tree's position
- Vector3 closestTreePosition = new Vector3();
- // Let's find the closest tree to the place we chopped and hit something
- int closestTreeIndex = 0;
- for (int i = 0; i < treeInstances.Length; i++)
- {
- TreeInstance currentTree = treeInstances[i];
- // The the actual world position of the current tree we are checking
- Vector3 currentTreeWorldPosition = Vector3.Scale(currentTree.position, terrain.size) + Terrain.activeTerrain.transform.position;
-
- // Find the distance between the current tree and whatever we hit when chopping
- float distance = Vector3.Distance(currentTreeWorldPosition, hit.point);
-
- // Is this tree even closer?
- if (distance < maxDistance)
- {
- maxDistance = distance;
- closestTreeIndex = i;
- closestTreePosition = currentTreeWorldPosition;
- }
- }
-
-
- // replace the tree prototypeIndex
- var temptree = new TreeInstance();
- temptree.prototypeIndex = 6;
- temptree.position = treeInstances[closestTreeIndex].position;
- temptree.color = new Color(1,1,1,1);
- temptree.lightmapColor = new Color(1,1,1,1);//must add
- TreeInstances.Add(temptree);
-
- // Remove the tree from the terrain tree list
- TreeInstances.RemoveAt(closestTreeIndex);
-
- //set back treeInstances array
- terrain.treeInstances = TreeInstances.ToArray();
-
-
-
- // Now refresh the terrain, getting rid of the darn collider
- float[,] heights = terrain.GetHeights(0, 0, 0, 0);
- terrain.SetHeights(0, 0, heights);
-
- // Terrain.activeTerrain.collider.enabled = false;
- // Terrain.activeTerrain.collider.enabled = true;
-
- // Terrain.activeTerrain.Flush();
- // Put a falling tree in its place
- Instantiate(FallingTreePrefab, closestTreePosition, Quaternion.identity);
- Debug.Log(DateTime.Now - start);
- }
- }
- }
- }

- temptree.color = new Color(1,1,1,1);
- temptree.lightmapColor = new Color(1,1,1,1);
这两句一定要加上,不然树木纹理显示不正常,也可以通过color设置透明效果。
- // Now refresh the terrain, getting rid of the darn collider
- float[,] heights = terrain.GetHeights(0, 0, 0, 0);
- terrain.SetHeights(0, 0, heights);
-
- // Terrain.activeTerrain.collider.enabled = false;
- // Terrain.activeTerrain.collider.enabled = true;
-
- // Terrain.activeTerrain.Flush();
上面三种方法好像都可以,测试结果有点记不清了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。