VioletaBabel
22일 : 간단한 슈팅 본문
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | //GameManagerScript.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameManagerScript : MonoBehaviour { public GameObject enemy = null; public GameObject enemyCounter = null; public GameObject bullets = null; public GameObject playerShip = null; public GameObject player = null; public GameObject mBullet = null; public GameObject effects = null; public GameObject boss = null; public GameObject nowBoss = null; public float shootTimer; public float waitingTime; public float gameSetTimer; public float gameSetWaitTime; public bool playerDie = false; public bool gameOver = false; public bool bossGen = false; public int bossLife; // Use this for initialization void Start() { bossLife = 500; effects = GameObject.FindWithTag("EffectManager"); shootTimer = 0; waitingTime = 0.3f; gameSetTimer = 0; gameSetWaitTime = 2.0f; makeEnemy(); makePlayer(); } // Update is called once per frame void Update() { shootTimer += Time.deltaTime; if (Input.GetKey(KeyCode.Space) == true) { if (shootTimer > waitingTime) { shootTimer = 0; Vector3 bulletStartPos = player.transform.position + new Vector3(0, 80, 0); var bullet = Instantiate(mBullet, bulletStartPos, Quaternion.identity); var bul = bullet.GetComponent<Bullet>(); bul.mDir = new Vector3(0, 1, 0); bul.playersBullet = true; bul.transform.parent = bullets.transform; } } if (Input.GetKey(KeyCode.LeftArrow)) { if (player.transform.position.x > -955) player.transform.Translate(-Time.deltaTime * 600.0f, 0, 0); } else if (Input.GetKey(KeyCode.RightArrow)) { if (player.transform.position.x < 955) player.transform.Translate(Time.deltaTime * 600.0f, 0, 0); } if (Input.GetKey(KeyCode.UpArrow)) { if (player.transform.position.y < 100) player.transform.Translate(0, 0, Time.deltaTime * 600.0f); } else if (Input.GetKey(KeyCode.DownArrow)) { if (player.transform.position.y > -400) player.transform.Translate(0, 0, -Time.deltaTime * 600.0f); } if(enemyCounter.transform.childCount <= 0 && !bossGen) { makeBoss(); bossGen = true; } if (playerDie || (enemyCounter.transform.childCount <= 0 && bossGen)) gameOver = true; if (gameOver) { gameSetTimer += Time.deltaTime; if (gameSetWaitTime < gameSetTimer) { gameSetTimer = 0; if (enemyCounter.transform.childCount <= 0) { foreach (Transform c in effects.transform) GameObject.Destroy(c.gameObject); foreach (Transform c in bullets.transform) GameObject.Destroy(c.gameObject); makeEnemy(); } if (playerDie) { foreach (Transform c in effects.transform) GameObject.Destroy(c.gameObject); foreach (Transform c in bullets.transform) GameObject.Destroy(c.gameObject); foreach (Transform c in enemyCounter.transform) GameObject.Destroy(c.gameObject); playerDie = false; makeEnemy(); makePlayer(); bossGen = false; bossLife = 500; } gameOver = false; } } } void makeEnemy() { for (int i = 0; i < 7; ++i) { GameObject nowEnemy = GameObject.Instantiate(enemy, new Vector3(-450 + (150 * i), 300, 100), Quaternion.Euler(90, 0, 0)); nowEnemy.transform.parent = enemyCounter.transform; } } void makePlayer() { player = GameObject.Instantiate(playerShip, new Vector3(0, -320, 100), Quaternion.Euler(270, 0, 0)); player.transform.parent = this.transform; } void makeBoss() { GameObject nowBoss = GameObject.Instantiate(boss, new Vector3(0, 300, 100), Quaternion.Euler(90, 0, 0)); nowBoss.transform.parent = enemyCounter.transform; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | //Bullet.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { public Vector3 mDir = new Vector3(0, 0, 0); public GameObject GameManager = null; public bool playersBullet = false; public GameObject explosion = null; public GameObject effects = null; public GameObject bossDamEffect = null; // Use this for initialization void Start() { GameManager = GameObject.FindWithTag("GameManager"); effects = GameObject.FindWithTag("EffectManager"); } // Update is called once per frame void Update() { transform.Translate(mDir * Time.deltaTime * 250.0f); if (transform.position.x < -1000 || transform.position.y > 600 || transform.position.x > 1000 || transform.position.y < -600) Destroy(gameObject); } void OnTriggerEnter(Collider other) { if (playersBullet) { if (other.gameObject.name != "PlayerShip(Clone)") { if (other.gameObject.name == "EnemyShip(Clone)") { GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity); ef.transform.parent = effects.transform; Destroy(other.gameObject); Destroy(gameObject); } else if(other.gameObject.name == "Sphere(Clone)") { Destroy(other.gameObject); Destroy(gameObject); } else if(other.gameObject.name == "Boss(Clone)") { if (--(GameManager.GetComponent<GameManagerScript>().bossLife) <= 0) { GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity); ef.transform.parent = effects.transform; Destroy(other.gameObject); } else { GameObject ef = Instantiate(bossDamEffect, transform.position+ new Vector3(0,100,-100), Quaternion.identity); ef.transform.parent = effects.transform; } Destroy(gameObject); } } } else { if(other.gameObject.name == "PlayerShip(Clone)") { GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity); ef.transform.parent = effects.transform; GameManager.GetComponent<GameManagerScript>().playerDie = true; Destroy(other.gameObject); Destroy(gameObject); } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | //EnemyShipScript.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyShipScript : MonoBehaviour { public float shootTimer; public float waitingTime; public float moveTimer; public float reverseTime; public bool left = true; public GameObject mBullet = null; public GameObject bullets = null; // Use this for initialization void Start() { bullets = GameObject.FindWithTag("BulletManager"); shootTimer = 0; waitingTime = 0.5f; moveTimer = 3.0f; reverseTime = 6.0f; } // Update is called once per frame void Update() { shootTimer += Time.deltaTime; moveTimer += Time.deltaTime; if (shootTimer > waitingTime) { shootTimer -= waitingTime; Vector3 bulletStartPos = transform.position + new Vector3(0, -80, 0); var bullet = Instantiate(mBullet, bulletStartPos, Quaternion.identity); var bul = bullet.GetComponent<Bullet>(); bul.mDir = new Vector3(0, -1, 0); bul.transform.parent = bullets.transform; } if(moveTimer > reverseTime) { moveTimer -= reverseTime; if (left) left = false; else left = true; } if(left) transform.Translate(-Time.deltaTime * 150.0f, 0, 0); else transform.Translate(Time.deltaTime * 150.0f, 0, 0); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | //BossShipScript.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class BossShipScript : MonoBehaviour { public float shootTimer; public float waitingTime; public GameObject bossBullet = null; public GameObject bullets = null; public GameObject effects = null; // Use this for initialization void Start() { bullets = GameObject.FindWithTag("BulletManager"); effects = GameObject.FindWithTag("EffectManager"); shootTimer = 10.0f; waitingTime = 10.0f; } // Update is called once per frame void Update() { shootTimer += Time.deltaTime; if (shootTimer > waitingTime) { shootTimer -= waitingTime; Vector3 bulletStartPos = transform.position + new Vector3(0, -80, 0); var bullet = Instantiate(bossBullet, bulletStartPos, Quaternion.identity); var bul = bullet.GetComponent<BossBullet>(); bul.mDir = new Vector3(0, -1, 0); bul.transform.parent = bullets.transform; foreach (Transform c in effects.transform) GameObject.Destroy(c.gameObject); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | //BossBullet.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class BossBullet : MonoBehaviour { public Vector3 mDir = new Vector3(0, 0, 0); public GameObject GameManager = null; public bool playersBullet = false; public GameObject explosion = null; public GameObject effects = null; public float slow; public bool bomb = false; public float bombTime; public float bombSetTime; public float ang; public int count; public float alpha; public GameObject mBullet = null; public GameObject bullets = null; // Use this for initialization void Start () { bullets = GameObject.FindWithTag("BulletManager"); count = 0; alpha = 1.0f; ang = Random.Range(0, 359.9f); slow = 250.0f; bombSetTime = 0.05f; GameManager = GameObject.FindWithTag("GameManager"); effects = GameObject.FindWithTag("EffectManager"); } // Update is called once per frame void Update () { if (!bomb) { transform.Translate(mDir * Time.deltaTime * slow); if (slow >= 0) slow -= 1.0f; else { slow = 0; bomb = true; } } else { bombTime += Time.deltaTime; if(count>=75) { Destroy(gameObject); } else if(count >= 50) { GameObject ef = Instantiate(explosion, transform.position, Quaternion.identity); ef.transform.parent = effects.transform; ++count; } else if(bombTime >= bombSetTime) { bombTime -= bombSetTime; Vector3 bulletStartPos = transform.position; var bullet = Instantiate(mBullet, bulletStartPos, Quaternion.identity); var bul = bullet.GetComponent<Bullet>(); bul.mDir = new Vector3(Mathf.Sin(ang/2), Mathf.Cos(ang/2),0); bul.mDir.Normalize(); bul.transform.parent = bullets.transform; ang += Random.Range(26.0f,40.0f); if (ang >= 360.0f) ang -= 360.0f; ++count; } } } } | cs |
'BCA > 4. Unity' 카테고리의 다른 글
26일 : 풀링과 싱글톤 & 연습용 슈팅게임 발전 (0) | 2018.03.23 |
---|---|
25일 : 연습용 슈팅게임 완성 (0) | 2018.03.22 |
24일 : dotween 이용, 2D슈팅게임 (0) | 2018.03.21 |
23일 : 체력바 추가 및 보스 강화 (0) | 2018.03.20 |
21일 : 벽돌 깨기 게임 (0) | 2018.03.16 |
Comments