SFE_prefabGenerator.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 thisManyTimes:int=3;
  4. var overThisTime:float=1.0;
  5. var generateOneInstantly:boolean=false;
  6. var xWidth:float; // define the square where prefabs will be generated
  7. var yWidth:float;
  8. var zWidth:float;
  9. var xRotMax:float; // define maximum rotation of each prefab
  10. var yRotMax:float=180;
  11. var zRotMax:float;
  12. var allUseSameRotation:boolean=false;
  13. private var allRotationDecided:boolean=false;
  14. var detachToWorld:boolean=true;
  15. private var x_cur:float; // these are used in the random palcement process
  16. private var y_cur:float;
  17. private var z_cur:float;
  18. private var xRotCur:float; // these are used in the random protation process
  19. private var yRotCur:float;
  20. private var zRotCur:float;
  21. private var timeCounter:float; // counts the time :p
  22. private var effectCounter:int; // you will guess ti
  23. private var trigger:float; // trigger: at which interwals should we generate a particle
  24. function Start () {
  25. if (thisManyTimes<1) thisManyTimes=1; //hack to avoid division with zero and negative numbers
  26. trigger=overThisTime/thisManyTimes; //define the intervals of time of the prefab generation.
  27. }
  28. function Update () {
  29. timeCounter+=Time.deltaTime;
  30. if (generateOneInstantly==true)
  31. {
  32. timeCounter=trigger;
  33. generateOneInstantly=false;
  34. }
  35. if(timeCounter>=trigger&&effectCounter<thisManyTimes)
  36. {
  37. rndNr=Mathf.Floor(Random.value*createThis.length); //decide which prefab to create
  38. x_cur=transform.position.x+(Random.value*xWidth)-(xWidth*0.5); // decide an actual place
  39. y_cur=transform.position.y+(Random.value*yWidth)-(yWidth*0.5);
  40. z_cur=transform.position.z+(Random.value*zWidth)-(zWidth*0.5);
  41. if(allUseSameRotation==false||allRotationDecided==false) // basically this plays only once if allRotationDecided=true, otherwise it plays all the time
  42. {
  43. xRotCur=transform.rotation.x+(Random.value*xRotMax*2)-(xRotMax); // decide rotation
  44. yRotCur=transform.rotation.y+(Random.value*yRotMax*2)-(yRotMax);
  45. zRotCur=transform.rotation.z+(Random.value*zRotMax*2)-(zRotMax);
  46. allRotationDecided=true;
  47. }
  48. var justCreated:GameObject=Instantiate(createThis[rndNr], Vector3(x_cur, y_cur, z_cur), transform.rotation); //create the prefab
  49. justCreated.transform.Rotate(xRotCur, yRotCur, zRotCur);
  50. if(detachToWorld==false) // if needed we attach the freshly generated prefab to the object that is holding this script
  51. {
  52. justCreated.transform.parent=transform;
  53. }
  54. timeCounter-=trigger; //administration :p
  55. effectCounter+=1;
  56. }
  57. }
  58. function OnDrawGizmosSelected () {
  59. Gizmos.color = Color (1,1,1,.2);
  60. Gizmos.DrawCube (transform.position, Vector3 (xWidth, yWidth, zWidth));
  61. if (xWidth==0 && yWidth==0 && zWidth==0) Gizmos.DrawSphere (transform.position, 0.5);
  62. }