Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class RandomInside : MonoBehaviour {
- public GameObject Prefab;
- public int NumberOfPrefabs = 50;
- public float Factor = 0.5f;
- void Start () {
- for (int i = 0; i <= NumberOfPrefabs; i++)
- {
- var obj = Instantiate(Prefab,GetRandomInsideTheMesh(),Quaternion.identity);
- obj.transform.parent = gameObject.transform;
- }
- }
- public Vector3 GetRandomInsideTheMesh(){
- Mesh mesh = gameObject.GetComponent<MeshFilter>().mesh;
- Bounds bounds = mesh.bounds;
- var pos = transform.position;
- var scale = transform.localScale;
- float minX = pos.x - scale.x * bounds.size.x * Factor;
- float minY = pos.y - scale.y * bounds.size.y * Factor;
- float minZ = pos.z - scale.z * bounds.size.z * Factor;
- Vector3 RandomArea = new Vector3(Random.Range (minX, -minX),
- Random.Range (minY, -minY),
- Random.Range (minZ, -minZ));
- return RandomArea;
- }
- }
Add Comment
Please, Sign In to add comment