-
Notifications
You must be signed in to change notification settings - Fork 1
/
Spawner.cs
53 lines (40 loc) · 2.22 KB
/
Spawner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System.Collections.Generic;
using Random = UnityEngine.Random;
using UnityEngine;
namespace PrefabSpawner {
public class Spawner : MonoBehaviour {
[SerializeField] private GameObject[] prefabs;
[SerializeField] private GameObject prefabParent;
[SerializeField] private GameObject terrain;
private Dictionary<GameObject, Vector3> TreesPosition;
private Mesh _mesh;
public void SpawnThemAll() {
_mesh = terrain.GetComponent<MeshCollider>().sharedMesh;
TreesPosition = new Dictionary<GameObject, Vector3>();
foreach (var meshTriangleIndex in _mesh.triangles) {
var vertex1 = _mesh.vertices[_mesh.triangles[meshTriangleIndex]];
var vertex2 = _mesh.vertices[_mesh.triangles[meshTriangleIndex + 1]];
var vertex3 = _mesh.vertices[_mesh.triangles[meshTriangleIndex + 2]];
var random1 = RandomVector3(vertex1, vertex2);
var random2 = RandomVector3(random1, vertex3);
var randomPosition = transform.TransformPoint(random2);
if (Physics.Raycast(randomPosition+Vector3.up*10, Vector3.down, out RaycastHit raycastHit)) {
if (raycastHit.transform.tag.Equals("vegetationMask")) {
var spawnedPrefab = Instantiate(prefabs[Random.Range(0, prefabs.Length)], raycastHit.point, Quaternion.identity, prefabParent.transform);
spawnedPrefab.transform.rotation = new Quaternion(0, Random.Range(-1, 1), 0, 0);
TreesPosition.Add(spawnedPrefab, raycastHit.point);
}
}
}
Debug.Log("spawned "+TreesPosition.Count+ " trees");
}
private Vector3 RandomVector3(Vector3 min, Vector3 max) {
return new Vector3(Random.Range(min.x, max.x), Random.Range(min.y, max.y), Random.Range(min.z, max.z));
}
public void RemoveThemAll() {
foreach (Transform prefab in prefabParent.transform) {
DestroyImmediate(prefab.gameObject);
}
}
}
}