Advertisement
Guest User

PopSpheresUnity

a guest
Feb 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.73 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class popSpheres : MonoBehaviour {
  6.  
  7.     public int nbSpheres = 1000;
  8.     public Vector3 maxBounds = new Vector3(100f,100f,100);
  9.     public Vector3 minBounds = new Vector3(-100f,-100f,0f);
  10.  
  11.     private List<GameObject> spheres;
  12.     public Material sphereMat;
  13.  
  14.     public float velocMax=0.5f;
  15.     public float velocMin=0.1f;
  16.  
  17.     private List<float> velocities;
  18.     private List<Vector3> directions;
  19.  
  20.  
  21.     private float time = 0.0f;
  22.  
  23.     // Use this for initialization
  24.     void Start () {
  25.         if(sphereMat == null)
  26.             sphereMat = new Material(Shader.Find("Standard"));
  27.         spheres = new List<GameObject>(nbSpheres);
  28.         velocities = new List<float>(nbSpheres);
  29.         directions = new List<Vector3>(nbSpheres);
  30.  
  31.         for(int i=0;i<nbSpheres;i++){
  32.             GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  33.             spheres.Add(sphere);
  34.             Renderer sr = sphere.GetComponent<Renderer>();
  35.             sr.sharedMaterial = sphereMat;
  36.             sr.motionVectors = false;
  37.             sr.receiveShadows = false;
  38.             sr.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  39.  
  40.             sphere.transform.position = new Vector3 (Random.Range(minBounds.x,maxBounds.x),Random.Range(minBounds.y,maxBounds.y),Random.Range(minBounds.z,maxBounds.z));
  41.             sphere.transform.parent = transform;
  42.             sphere.transform.name = sphere.transform.name+"_"+i;
  43.             velocities.Add(Random.Range(velocMin, velocMax));
  44.             directions.Add(new Vector3(Random.Range(-1,1),Random.Range(-1,1 ),Random.Range(-1,1 )).normalized);
  45.  
  46.         }
  47.     }
  48.  
  49.     void Update(){
  50.  
  51.         time += Time.deltaTime;
  52.  
  53.         for(int i=0;i<nbSpheres;i++){
  54.             spheres[i].transform.position += Time.deltaTime * (directions[i]*velocities[i]);
  55.         }
  56.         //Check bounds ...
  57.  
  58.        
  59.     }
  60.    
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement