VioletaBabel
21일 : 벽돌 깨기 게임 본문
유니티 시작
rigidbody = 중력이 작용하나봄
assets에서 create-physic material을 만들어 bounciness에는 튀게 할 정도 (1.0이면 완전탄성충돌)
상자와 공에 각자 이 피직스를 넣어주자.
그리고 실행하면 둘이 부딪히면 통통 튄다.
bounce combine에서 average를 하면 양쪽에 들어간 바운시니스의 평균값, 멀티플은 곱값 등으로 적용됨.
dynamic friction = 마찰력
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { if(Input.GetKey(KeyCode.LeftArrow)) { transform.Rotate(new Vector3(0, 0, 1), Time.deltaTime * 40.0f); } else if(Input.GetKey(KeyCode.RightArrow)) { transform.Rotate(new Vector3(0, 0, -1), Time.deltaTime * 40.0f); } } } | cs |
(Z축기준)블럭 회전
=========
벽돌깨기 게임 만들기
create-physic material 후, Friction은 모두 0.
Bounciness 1
Bounce Combine Maximum.
그리고 Sphere, Cube, Wall Object의 Box(또는 Sphere) Collider의 Material 부분에 해당 physic material을 넣어준다.
Sphere는 물리 작용을 받아야하므로 Inspector의 Add Component를 눌러 Rigidbody를 넣어준다.
단, Rigidbody에는 Use Gravity는 체크를 해제하여야 한다. 우리는 중력을 쓰는게 아니기 때문.
그리고 Block과 Wall_Sphere(없앨 블럭과 벽에 붙은 구)를 만들고, Assets에 드래그 후 Hierarchy에서는 지운다.
그리고 게임 자체를 의미하여 블럭 생성을 하고 게임 클리어 시 스스로를 죽이고 다시 새로 만듦으로써 게임을 재시작할 GameObject도 만들어 똑같이 Assets에 프리팹으로 넣는다.
Block과 Wall_Sphere에도 physic material을 넣어주어야 한다.
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 | //Block.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class Block : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } private void OnCollisionEnter(Collision collision) { 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 | //MakeBlock.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class MakeBlock : MonoBehaviour { public GameObject block = null; public GameObject my = null; public GameObject cube = null; // Use this for initialization void Start() { for (int i = 0, k = 0; i < 6; ++i) for (int j = 1; j < 10; ++j, ++k) {//블럭 생성& 알록달록 징그럽게 색 넣어주기 GameObject nowBlock = GameObject.Instantiate(block, new Vector3(j - 5, 10 + i, 0), Quaternion.identity); nowBlock.transform.parent = this.transform; // 블럭을 Hierarchy의 GameObject의 자식으로 넣어줌. Renderer r = nowBlock.GetComponent<Renderer>();//블럭에 색이나 입히자 switch (k % 7)//요로케 { case 0: r.material.color = Color.black; break; case 1: r.material.color = Color.blue; break; case 2: r.material.color = Color.green; break; case 3: r.material.color = Color.magenta; break; case 4: r.material.color = Color.red; break; case 5: r.material.color = Color.yellow; break; case 6: r.material.color = Color.grey; break; } } } // Update is called once per frame void Update() { if (transform.childCount <= 0) // 블럭이 다 없어지면 { Destroy(gameObject); // 게임을 종료하고 GameObject.Instantiate(my, new Vector3(0, 0, 0), Quaternion.identity);//재시작해 Start호출 //다른클래스의 함수를 부를 것이라 그 클래스를 품은 cube에게서 클래스를 가져옴. var script = cube.GetComponent<NewBehaviourScript>(); script.rePosSphere();//날아가던 공 위치를 재설정해줌 } } } | 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 82 | //NewBehaviourScript.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class NewBehaviourScript : MonoBehaviour { public GameObject sphere = null; public bool wasStart = false; public GameObject wallSphere = null; // Use this for initialization void Start () { sphere.GetComponent<Renderer>().material.color = Color.black;//공은깜장 transform.GetComponent<Renderer>().material.color = Color.black;//밑에공받침대도깜장 for (int i = 0; i < 16; ++i) { if (i % 2 == 0) {//왼쪽 벽 오돌토돌 Instantiate(wallSphere, new Vector3(-5.5f, i * 1.25f + 1, 0), Quaternion.identity); } else {//오른 벽 오돌토돌 Instantiate(wallSphere, new Vector3(5.5f, i * 1.25f + 1, 0), Quaternion.identity); } } } // Update is called once per frame void Update () { if(Input.GetKey(KeyCode.LeftArrow)) {//왼쪽 화살표 눌렀는데 if (transform.position.x > -3) {//니 X좌표 -3보다 커 transform.Translate(-Time.deltaTime * 20.0f, 0, 0);//그럼 공받침대 이동 if(!wasStart) {//근데 게임이 시작도 안했어 var r = sphere.GetComponent<Rigidbody>();//그럼 공도 같이 가자 r.transform.Translate(-Time.deltaTime * 20.0f, 0, 0); } } } else if(Input.GetKey(KeyCode.RightArrow)) {//위랑 방향만 반대고 쌤쌤 if (transform.position.x < 3) { transform.Translate(Time.deltaTime * 20.0f, 0, 0); if (!wasStart) { var r = sphere.GetComponent<Rigidbody>(); r.transform.Translate(Time.deltaTime * 20.0f, 0, 0); } } } if(Input.GetKeyDown(KeyCode.Space)) {//스페이스 바 누르면 시작 if (!wasStart)//아직 시작 안됐는데 눌렀으면 {//공이 오른쪽과 위로 400.0f 씩의 힘을 받음 var r = sphere.GetComponent<Rigidbody>(); r.AddForce(400.0f, 400.0f, 0); wasStart = true; } } if(sphere.transform.position.y < -3) {//공이 밑으로 떨어지면 위치 재설정 rePosSphere(); } } public void rePosSphere() {//공 위치를 재설정해주는 함수 Vector3 now = transform.position; now.y += 1;//공받침대보다 y좌표가 1 큼 sphere.transform.position = now; // 공 위치 설정 var r = sphere.GetComponent<Rigidbody>(); r.velocity = Vector3.zero;//공 속도를 0으로 만듦 r.angularVelocity = Vector3.zero; wasStart = false; } } | cs |
New Behaviour Script는 Cube에,
Block은 Assets의 Block 프리팹에,
MakeBlock은 Assets의 GameObject 프리팹에 넣어준다.
그리고 Cube의 스크립트 항목의 Wall Sphere에 Wall_Sphere를
GameObject의 스크립트 항목에는 Block에 Block 프리팹, My에 GameObject 프리팹을 넣는다.
'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 |
| 22일 : 간단한 슈팅 (0) | 2018.03.19 |