SFE_continuousPrefabGenerator.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var createThis:GameObject[]; // list of possible prefabs
  2. private var rndNr:float; // this is for just a random number holder when we need it
  3. var thisManyTimesPerSec:float=1;
  4. var xWidth:float; // define the square where prefabs will be generated
  5. var yWidth:float;
  6. var zWidth:float;
  7. var xRotMax:float; // define maximum rotation of each prefab
  8. var yRotMax:float=180;
  9. var zRotMax:float;
  10. private var x_cur:float; // these are used in the random palcement process
  11. private var y_cur:float;
  12. private var z_cur:float;
  13. private var xRotCur:float; // these are used in the random protation process
  14. private var yRotCur:float;
  15. private var zRotCur:float;
  16. private var timeCounter:float; // counts the time :p
  17. private var effectCounter:int; // you will guess ti
  18. private var trigger:float; // trigger: at which interwals should we generate a prefab
  19. function Start () {
  20. if (thisManyTimesPerSec<0.1) thisManyTimesPerSec=0.1; //hack to avoid division with zero and negative numbers
  21. trigger=1/thisManyTimesPerSec; //define the intervals of time of the prefab generation.
  22. //Debug.Log(trigger);
  23. }
  24. function Update () {
  25. timeCounter+=Time.deltaTime;
  26. if(timeCounter>trigger)
  27. {
  28. rndNr=Mathf.Floor(Random.value*createThis.length); //decide which prefab to create
  29. x_cur=transform.position.x+(Random.value*xWidth)-(xWidth*0.5); // decide an actual place
  30. y_cur=transform.position.y+(Random.value*yWidth)-(yWidth*0.5);
  31. z_cur=transform.position.z+(Random.value*zWidth)-(zWidth*0.5);
  32. xRotCur=transform.rotation.x+(Random.value*xRotMax*2)-(xRotMax); // decide rotation
  33. yRotCur=transform.rotation.y+(Random.value*yRotMax*2)-(yRotMax);
  34. zRotCur=transform.rotation.z+(Random.value*zRotMax*2)-(zRotMax);
  35. var justCreated:GameObject=Instantiate(createThis[rndNr], Vector3(x_cur, y_cur, z_cur), transform.rotation); //create the prefab
  36. justCreated.transform.Rotate(xRotCur, yRotCur, zRotCur);
  37. timeCounter-=trigger; //administration :p
  38. }
  39. }