BULLET
How do I use B U L L E T
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: GameCtrl.cs
Copy
22 public void BulletHitEnemy(Transform enemy)
23 {
24 // destroy the enemy
25 Destroy(enemy.gameObject);
26 }
File name: MobileUICtrl.cs
Copy
32 public void MobileFireBullets()
33 {
34 playerCtrl.MobileFireBullets();
35 }
File name: PlayerBulletCtrl.cs
Copy
20 private void OnCollisionEnter2D(Collision2D collision)
21 {
22 if (collision.gameObject.CompareTag("Enemy"))
23 {
24 GameCtrl.instance.BulletHitEnemy(collision.gameObject.transform);
25 Destroy(gameObject);
26 }
27 else if (collision.gameObject.CompareTag("Player"))
28 {
29 Destroy(gameObject);
30 }
31 }
File name: PlayerCtrl.cs
Copy
37 void Update () {
38 isGrounded = Physics2D.OverlapBox(new Vector2(feet.position.x, feet.position.y), new Vector2(boxWidth, boxHeight), 360.0f, whatIsGround);
39
40 float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be 1, -1 or 0
41
42 playerSpeed *= speedBoost;
43
44 if (playerSpeed != 0)
45 MovePlayer(playerSpeed);
46 else
47 StopMoving();
48
49 if (Input.GetButtonDown("Jump"))
50 Jump();
51
52 if (Input.GetButtonDown("Fire1"))
53 {
54 FireBullets();
55 }
56
57 ShowFalling();
58
59 if (leftPressed)
60 MovePlayer(-speedBoost);
61
62 if (rightPressed)
63 MovePlayer(speedBoost);
64 }
File name: PlayerCtrl.cs
Copy
128 void FireBullets()
129 {
130 if (sr.flipX)
131 {
132 Instantiate(leftbullet, leftbulletSpawnPos.position, Quaternion.identity);
133 }
134
135 if (!sr.flipX) {
136 Instantiate(rightbullet, rightbulletSpawnPos.position, Quaternion.identity);
137 }
138 }
File name: BossShoot.cs
Copy
19 void Update () {
20 if(isReadyToShoot){
21 fireRate = Random.Range(4, 6);
22 firstDelay += Time.deltaTime;
23 if (firstDelay >= fireRate) {
24 if (!fireBullet) {
25 fireBullet = true;
26 if (GameObject.FindGameObjectWithTag ("Player") != null) {
27 GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
28 Transform target = GameObject.FindGameObjectWithTag ("Player").transform;
29 newBullet.GetComponent
30 } else {
31 GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
32 newBullet.GetComponent
33 }
34 }
35
36 if (fireBullet) {
37 secondDelay += Time.deltaTime;
38 if (secondDelay >= 0.5f) {
39 secondDelay = 0;
40 firstDelay = 0;
41 if (GameObject.FindGameObjectWithTag ("Player") != null) {
42 GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
43 Transform target = GameObject.FindGameObjectWithTag ("Player").transform;
44 newBullet.GetComponent
45 } else {
46 GameObject newBullet = Instantiate (bullet, new Vector3 (transform.position.x, 0.3f, transform.position.z), Quaternion.identity) as GameObject;
47 newBullet.GetComponent
48 }
49 fireBullet = false;
50 }
51 }
52 }
53
54 }
55
56
57 }
File name: GameBoundary.cs
Copy
17 void OnTriggerEnter2D(Collider2D collider){
18 if(collider.CompareTag("Player Bullet") || collider.CompareTag("Enemy") || collider.CompareTag("Coin") || collider.CompareTag("Boss Bullet")){
19 Destroy (collider.gameObject);
20 }
21
22 }
File name: GameplayController.cs
Copy
125 void ClearAllEnemies(){
126 GameObject[] enemies = GameObject.FindGameObjectsWithTag ("Enemy");
127 GameObject[] bossbullets = GameObject.FindGameObjectsWithTag ("Boss Bullet");
128 GameObject[] coins = GameObject.FindGameObjectsWithTag ("Coin");
129 GameObject boss = GameObject.FindGameObjectWithTag ("Boss");
130
131 if(boss != null){
132 Destroy (boss);
133 }
134
135 if(enemies != null){
136 foreach (GameObject enemy in enemies) {
137 Destroy (enemy);
138 }
139 }
140
141 if(bossbullets != null){
142 foreach (GameObject bossBullet in bossbullets) {
143 Destroy (bossBullet);
144 }
145 }
146
147 if(coins != null){
148 foreach (GameObject coin in coins) {
149 Destroy (coin);
150 }
151 }
152
153 }
File name: PlayerShoot.cs
Copy
24 void Update () {
25 if (GameplayController.instance.gameInProgress) {
26 ShootBullet ();
27 }
28 }
Download file with original file name:BULLET
BULLET 127 lượt xem
Gõ tìm kiếm nhanh...