Guest User

Untitled

a guest
Mar 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RandomInside : MonoBehaviour {
  6. public GameObject Prefab;
  7. public int NumberOfPrefabs = 50;
  8. public float Factor = 0.5f;
  9.  
  10. void Start () {
  11. for (int i = 0; i <= NumberOfPrefabs; i++)
  12. {
  13. var obj = Instantiate(Prefab,GetRandomInsideTheMesh(),Quaternion.identity);
  14. obj.transform.parent = gameObject.transform;
  15. }
  16. }
  17.  
  18. public Vector3 GetRandomInsideTheMesh(){
  19. Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;
  20. Bounds bounds = mesh.bounds;
  21. var pos = transform.position;
  22. var scale = transform.localScale;
  23. float minX = pos.x - scale.x * bounds.size.x * Factor;
  24. float minY = pos.y - scale.y * bounds.size.y * Factor;
  25. float minZ = pos.z - scale.z * bounds.size.z * Factor;
  26.  
  27. Vector3 RandomArea = new Vector3(Random.Range (minX, -minX),
  28. Random.Range (minY, -minY),
  29. Random.Range (minZ, -minZ));
  30. return RandomArea;
  31. }
  32. }
Add Comment
Please, Sign In to add comment