ShootBullet









How do I use Shoot Bullet
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: PlayerShoot.cs Copy
24  void Update () {
25   if (GameplayController.instance.gameInProgress) {
26    ShootBullet ();
27   }
28  }
File name: PlayerShoot.cs Copy
30  void ShootBullet(){
31   if (GameController.instance != null) {
32    switch(GameController.instance.weaponLevel){
33    case 1:
34     time += Time.deltaTime;
35
36     if (time >= delay) {
37      time = 0;
38      Vector3[] position = new[] {
39       new Vector3 (transform.position.x - 0.15f, transform.position.y + 0.4f, 0),
40       new Vector3 (transform.position.x + 0.15f, transform.position.y + 0.4f, 0)
41      };
42
43      for (int i = 0; i < position.Length; i++) {
44       GameObject newBullet = Instantiate (bullet, position [i], Quaternion.identity) as GameObject;
45       newBullet.GetComponent ().velocity = Vector2.up * speed;
46      }
47
48      if (GameController.instance != null) {
49       if (GameController.instance.isMusicOn) {
50        transform.GetComponent ().PlayOneShot (shoot);
51       }
52      }
53     }
54     break;
55
56    case 2:
57     time += Time.deltaTime;
58
59     if (time >= delay) {
60      time = 0;
61      Vector3[] position = new[] {
62       new Vector3 (transform.position.x - 0.15f, transform.position.y + 0.4f, 0),
63       new Vector3 (transform.position.x + 0.15f, transform.position.y + 0.4f, 0),
64       new Vector3 (transform.position.x, transform.position.y + 0.4f, 0)
65      };
66
67      for (int i = 0; i < position.Length; i++) {
68       GameObject newBullet = Instantiate (bullet, position [i], Quaternion.identity) as GameObject;
69       newBullet.GetComponent ().velocity = Vector2.up * speed;
70      }
71
72      if (GameController.instance != null) {
73       if (GameController.instance.isMusicOn) {
74        transform.GetComponent ().PlayOneShot (shoot);
75       }
76      }
77     }
78     break;
79
80    case 3:
81     time += Time.deltaTime;
82
83     if (time >= delay) {
84      time = 0;
85      Vector3[] position = new[] {
86       new Vector3 (transform.position.x - 0.05f, transform.position.y + 0.4f, 0),
87       new Vector3 (transform.position.x - 0.15f, transform.position.y + 0.4f, 0),
88       new Vector3 (transform.position.x + 0.05f, transform.position.y + 0.4f, 0),
89       new Vector3 (transform.position.x + 0.15f, transform.position.y + 0.4f, 0)
90      };
91
92      for (int i = 0; i < position.Length; i++) {
93       GameObject newBullet = Instantiate (bullet, position [i], Quaternion.identity) as GameObject;
94       newBullet.GetComponent ().velocity = Vector2.up * speed;
95      }
96
97      if (GameController.instance != null) {
98       if (GameController.instance.isMusicOn) {
99        transform.GetComponent ().PlayOneShot (shoot);
100       }
101      }
102     }
103     break;
104
105    case 4:
106     time += Time.deltaTime;
107
108     if (time >= delay) {
109      time = 0;
110      Vector3[] position = new[] {
111       new Vector3 (transform.position.x - 0.10f, transform.position.y + 0.4f, 0),
112       new Vector3 (transform.position.x - 0.20f, transform.position.y + 0.4f, 0),
113       new Vector3 (transform.position.x + 0.10f, transform.position.y + 0.4f, 0),
114       new Vector3 (transform.position.x + 0.20f, transform.position.y + 0.4f, 0),
115       new Vector3 (transform.position.x, transform.position.y + 0.4f, 0)
116      };
117
118      for (int i = 0; i < position.Length; i++) {
119       GameObject newBullet = Instantiate (bullet, position [i], Quaternion.identity) as GameObject;
120       newBullet.GetComponent ().velocity = Vector2.up * speed;
121      }
122
123      if (GameController.instance != null) {
124       if (GameController.instance.isMusicOn) {
125        transform.GetComponent ().PlayOneShot (shoot);
126       }
127      }
128     }
129     break;
130
131    case 5:
132     time += Time.deltaTime;
133
134     if (time >= delay) {
135      time = 0;
136      Vector3[] position = new[] {
137       new Vector3 (transform.position.x - 0.05f, transform.position.y + 0.4f, 0),
138       new Vector3 (transform.position.x - 0.15f, transform.position.y + 0.4f, 0),
139       new Vector3 (transform.position.x - 0.25f, transform.position.y + 0.4f, 0),
140       new Vector3 (transform.position.x + 0.05f, transform.position.y + 0.4f, 0),
141       new Vector3 (transform.position.x + 0.15f, transform.position.y + 0.4f, 0),
142       new Vector3 (transform.position.x + 0.25f, transform.position.y + 0.4f, 0)
143      };
144
145      for (int i = 0; i < position.Length; i++) {
146       GameObject newBullet = Instantiate (bullet, position [i], Quaternion.identity) as GameObject;
147       newBullet.GetComponent ().velocity = Vector2.up * speed;
148      }
149
150      if (GameController.instance != null) {
151       if (GameController.instance.isMusicOn) {
152        transform.GetComponent ().PlayOneShot (shoot);
153       }
154      }
155     }
156     break;
157    }
158   }
159  }

ShootBullet 125 lượt xem

Gõ tìm kiếm nhanh...