Randomization
How do I use Randomization
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: BossMoving.cs
Copy
31 IEnumerator Moving()
32 {
33 yield return new WaitForSeconds(2);
34 nextPoint = bossPoints[3];
35 yield return new WaitForSeconds(2);
36 while (true)
37 {
38 nextPoint = bossPoints[rand.Randomization_1(0, 20, 40, 60, 73, 86, 99)];
39 yield return new WaitForSeconds(6);
40 }
41 }
File name: SpawnManager.cs
Copy
42 IEnumerator SpawningRoutine()
43 {
44 yield return new WaitForSeconds(3);
45
46 while (!UIgame.scoreAchived)
47 {
48 GameObject enemy = null;
49
50 //FIRST STAGE OF SPAWNING
51 //CHOOSE ENEMY FOR SPAWN
52 int r = rand.Randomization_1(0, 10, 80, 99);
53 if (r == 0)
54 {
55 enemy = pools.GetPoolableObject("enemy_01");
56 }
57 else if (r == 1)
58 {
59 enemy = pools.GetPoolableObject("enemy_02");
60 }
61 else
62 {
63 enemy = pools.GetPoolableObject("enemy_03");
64 }
65
66 //SECOND STAGE OF SPAWNING
67 //SPAWN CHOSEN ENEMY IN RANDOM POINT ON TOP SCREEN SIDE
68 r = Random.Range(0, pointsPositions.Length);
69
70 if (enemy != null)
71 {
72 enemy.SetActive(true);
73 enemy.GetComponent
74 new Vector3(0,0,-1));
75 }
76
77 int spawnDelay;
78 if (SystemScr.difficultyIsHard)
79 spawnDelay = 1;
80 else
81 spawnDelay = Random.Range(3,5);
82
83 yield return new WaitForSeconds(spawnDelay);
84 }
85
86 //BOSS ACTIVATION
87 pools.DeleteEnemiesAndBullets();
88 CameraMotor.speedScreen = 0;
89 Camera.main.GetComponent
90 Camera.main.GetComponent
91 Camera.main.backgroundColor = Color.black;
92 GameObject.FindObjectOfType
93
94 yield return new WaitForSeconds(6);
95
96 pools.AcivateBoss();
97 }
File name: SystemScr.cs
Copy
6 public static int Randomization_1(this Random rand, params int[] randBorders)
7 {
8 int r = Random.Range(0,100);
9 int returningValue = 0;
10
11 for (int i = 0; i < randBorders.Length-1; i++)
12 {
13 if (r > randBorders[i] && r < randBorders[i + 1])
14 {
15 returningValue = i;
16 break;
17 }
18 }
19
20 return returningValue;
21 }
File name: SystemScr.cs
Copy
23 public static int Randomization_2(this Random rand, params int[] digits)
24 {
25 return Random.Range(0, digits.Length);
26 }
File name: Bonus.cs
Copy
18 void RandomizeBonus()
19 {
20 int r = random.Randomization_1(0, 10, 50, 80, 99);
21 thisBonus = (BonusesEnum)r;
22 rend.sharedMaterial = materials[r];
23 }
Randomization 147 lượt xem
Gõ tìm kiếm nhanh...