7 7 Object Collider Owl Bird Sphere Collider
7. 7 Object에 Collider 추가 - Owl, Bird는 Sphere Collider 추가 - Branch, Gift는 Box Collider추가 - Owl에는 Rigidbody도 추가. Is Trigger 는 ON, Use Gravity는 해제
7. 11 게임 화면 스크롤 using Unity. Engine; using System. Collections; public class Cs. Sky : Mono. Behaviour { float speed = 0. 03 f; void Update () { //오프셋이용. float ofs. X = speed * Time. time; transform. renderer. material. main. Texture. Offset = new Vector 2(ofs. X, 0); } } // end of class
7. 12 화면을 벗어난 나뭇가지 제거 using Unity. Engine; using System. Collections; public class Cs. Branch : Mono. Behaviour { void Update () { // 월드 좌표를 스크린 좌표로 변환 Vector 3 view = Camera. main. World. To. Screen. Point(transform. position); if (view. y < -50) { //화면 아래로 내려가면 제거 Destroy(game. Object); } } } // end of class
7. 13 Gift 스크립트 작성 void Display. Score () { // Gift의 Tag를 점수로 변환 int n = int. Parse(transform. tag. Substring(4, 1)) * 500; // 점수 표시용 GUIText 만들기 Transform obj = Instantiate(txt. Score) as Transform; obj. gui. Text. text = "<size=22><color=green>+" + n + "</color></size>"; // World 좌표를 Viewport 좌표로 변환 var pos = Camera. main. World. To. Viewport. Point(transform. position); obj. position = pos; // Gift 제거 Destroy(game. Object);
7. 14 참새의 이동 public class Cs. Bird : Mono. Behaviour { public Transform txt. Score; int frm. Cnt = 6; int frm. Per. Sec; float frm. Delay; int frm. Num = 0; // 프리팹 // 전체 프레임 수 // 1초에 표시할 프레임 수 // 프레임 표시 간격 // 현재 표시할 프레임 번호 int speed; bool is. Drop = false; // 추락중인가? void Start () { speed = Random. Range(3, 7); frm. Per. Sec = Random. Range(10, 18); frm. Delay = 1. 0 f / frm. Per. Sec; } // 이동 속도
7. 14 참새의 이동 void Update () { float amt. Move = speed * Time. smooth. Delta. Time; if (!is. Drop) { Animate. Bird(); transform. Translate(Vector 3. right * amt. Move); } else { // 아래로 이동 transform. Translate(Vector 3. down * amt. Move, Space. World); } // 화면을 벗어난 오브젝트 제거 Vector 3 view = Camera. main. World. To. Screen. Point(transform. position); if (view. y < -50 || view. x > Screen. width + 50) { Destroy(game. Object); } }
7. 14 참새의 이동 void Drop. Bird () { is. Drop = true; // 참새 회전 transform. euler. Angles = new Vector 3(0, 0, 180); // 감점 표시 Transform obj = Instantiate(txt. Score) as Transform; obj. gui. Text. text = "<color=red><size=22>-1000</size></color>"; // World 좌표를 Viewport 좌표로 변환 var pos = Camera. main. World. To. Viewport. Point(transform. position); obj. position = pos; } - txt. Score변수에 txt. Score 프리팹 연결
7. 15 득점 및 감점 표시 void Start () { Start. Coroutine("Display. Score"); } void Update () { Vector 3 pos = transform. position; pos. y += 0. 001 f; transform. position = pos; } IEnumerator Display. Score() { yield return new Wait. For. Seconds(0. 5 f); for (float a = 1; a >= 0; a -= 0. 05 f) { transform. gui. Text. material. color = new Vector 4(1, 1, 1, a); yield return new Wait. For. Fixed. Update(); } Destroy(game. Object); }
7. 16 올빼미의 이동 public class Cs. Owl : Mono. Behaviour { public GUISkin skin; // GUI Skin public Transform branch; public Transform gift; public Transform bird; // 프리팹 public public // 효과음 및 배경음악 Audio. Clip snd. Jump; snd. Gift; snd. Bird; snd. Stage; snd. Over;
7. 16 올빼미의 이동 Transform sp. Point; Transform new. Branch; // Spawn Point // 새로만들 나뭇가지 int speed. Side = 10; int speed. Jump = 16; int gravity = 25; // 좌우 이동 속도 // 점프 속도 // 추락 속도 Vector 3 move. Dir = Vector 3. zero; int gift. Cnt = 0; float score = 0; float max. Y = 0; bool is. Dead = false; // 올빼미의 이동 방향 // 획득한 선물 수 // 득점 // 올빼미의 최대 높이
7. 17 게임 초기화 void Start () { // 모바일 단말기 설정 Screen. orientation = Screen. Orientation. Landscape. Right; Screen. sleep. Timeout = Sleep. Timeout. Never. Sleep; sp. Point = Game. Object. Find("sp. Point"). transform; // 나뭇가지 만들기 new. Branch = Instantiate(branch, sp. Point. position, sp. Point. rotation) as sform; Screen. show. Cursor = false; // 커서 감추기 } void Update () { if (is. Dead) return; Jump. Owl(); Move. Camera(); Make. Gift(); } // 올빼미 점프 // 올빼미 이동 // 카메라 이동 // 선물과 새 만들기
7. 19 올빼미의 점프 void Jump. Owl () { Raycast. Hit hit; // 올빼미 아래에 나뭇가지가 있는지 조사 if (Physics. Raycast(transform. position, Vector 3. down, out hit, 0. 9 f)) { if (hit. transform. tag == "BRANCH") { move. Dir. y = speed. Jump; Audio. Source. Play. Clip. At. Point(snd. Jump, transform. position); } } }
7. 20 올빼미의 좌우 이동 void Move. Owl () { // 올빼미 위치를 Screen 좌표로 변환 Vector 3 view = Camera. main. World. To. Screen. Point(transform. position); if (view. y < -50) { is. Dead = true; return; } // 화면 아래를 벗어나면 // 게임 오버 move. Dir. x = 0; // 올빼미의 좌우 이동 방향
7. 20 올빼미의 좌우 이동 // Mobile 처리 if (Application. platform == Runtime. Platform. Android || Application. platform == Runtime. Platform. IPhone. Player) { // 중력 가속도 센서 읽기 float x = Input. acceleration. x; // 왼쪽으로 기울였나? if (x < -0. 2 f && view. x > 35) { move. Dir. x = 2 * x * speed. Side; } // 오른쪽으로 기울였나? if (x > 0. 2 f && view. x < Screen. width - 35) { move. Dir. x = 2 * x * speed. Side; } } else { // Keyboard 읽기 float key = Input. Get. Axis("Horizontal"); if ((key < 0 && view. x > 35) || (key > 0 && view. x < Screen. width - 35)) { move. Dir. x = key * speed. Side; } }
7. 20 올빼미의 좌우 이동 // 매 프레임마다 점프 속도 감소 move. Dir. y -= gravity * Time. delta. Time; transform. Translate(move. Dir * Time. smooth. Delta. Time); // 올빼미의 날개 int n = 2; if (move. Dir. y < 0) n = 1; // 날개 펴기 // 날개 접기 transform. renderer. material. main. Texture = Resources. Load("owl" + n) as Texture 2 D; }
7. 21 카메라의 이동 void Move. Camera () { // 올빼미의 최대 높이 구하기 if (transform. position. y > max. Y) { max. Y = transform. position. y; // 카메라 위치 이동 Camera. main. transform. position = new Vector 3(0, max. Y - 2. 5 f, -10); score = max. Y * 1000; } // 가장 최근의 나뭇가지와 sp. Point와의 거리 구하기 if (sp. Point. position. y - new. Branch. position. y >= 4) { float x = Random. Range(-10 f, 10 f) * 0. 5 f; Vector 3 pos = new Vector 3(x, sp. Point. position. y, 0. 3 f); new. Branch = Instantiate(branch, pos, Quaternion. identity) as Transform; // 나뭇가지의 회전방향 설정 int mx = (Random. Range(0, 2) == 0) ? -1 : 1; int my = (Random. Range(0, 2) == 0) ? -1 : 1; new. Branch. renderer. material. main. Texture. Scale = new Vector 2(mx, my); } }
7. 22 Bird와 Gift 만들기 void Make. Gift () { if (Random. Range(1, 1000) < 980) return; Vector 3 pos = Vector 3. zero; // 오브젝트 표시 위치 pos. y = max. Y + Random. Range(4, 5. 5 f); if (Random. Range(0, 100) < 50) { pos. x = -12 f; Instantiate(bird, pos, Quaternion. identity); // 참새 만들기 } else { //선물 만들기 pos. x = Random. Range(-5 f, 5 f); Transform obj = Instantiate(gift, pos, Quaternion. identity) as Transform; int n = Random. Range(1, 4); obj. tag = "GIFT" + n; obj. game. Object. renderer. material. main. Texture = Resources. Load("gift" + n) as Texture 2 D; } }
7. 23 Gift의 개수 제한 if (Random. Range(0, 100) < 50) { // 참새 만들기 pos. x = -12 f; Instantiate(bird, pos, Quaternion. identity); } else { // 화면의 선물 수를 5개 이내로 제한 int n 1 = Game. Object. Find. Game. Objects. With. Tag("GIFT 1"). Length; int n 2 = Game. Object. Find. Game. Objects. With. Tag("GIFT 2"). Length; int n 3 = Game. Object. Find. Game. Objects. With. Tag("GIFT 3"). Length; if (n 1 + n 2 + n 3 >= 5) return; … }
7. 24 충돌의 판정 void On. Trigger. Enter (Collider coll) { switch (coll. transform. tag. Substring(0, 4)) { case "GIFT" : Audio. Source. Play. Clip. At. Point(snd. Gift, transform. position); // 득점 처리 int n = int. Parse(coll. transform. tag. Substring(4)); score += n * 500; gift. Cnt++; // 선물 제거 coll. transform. Send. Message("Display. Score", Send. Message. Options. Dont. Require. Recei break; case "BIRD" : if (coll. transform. euler. Angles. z != 0) return; Audio. Source. Play. Clip. At. Point(snd. Bird, transform. position); score -= 1000; // 참새 추락 처리 coll. transform. Send. Message("Drop. Bird", Send. Message. Options. Dont. Require. Receiver) break; } }
7. 25 득점 및 게임 오버 표시 void On. Trigger. Enter (Collider coll) { switch (coll. transform. tag. Substring(0, 4)) { case "GIFT" : Audio. Source. Play. Clip. At. Point(snd. Gift, transform. position); // 득점 처리 int n = int. Parse(coll. transform. tag. Substring(4)); score += n * 500; gift. Cnt++; // 선물 제거 coll. transform. Send. Message("Display. Score", Send. Message. Options. Dont. Require. Recei break; case "BIRD" : if (coll. transform. euler. Angles. z != 0) return; Audio. Source. Play. Clip. At. Point(snd. Bird, transform. position); score -= 1000; // 참새 추락 처리 coll. transform. Send. Message("Drop. Bird", Send. Message. Options. Dont. Require. Receiver) break; } }
7. 25 득점 및 게임 오버 표시 void On. GUI() { GUI. skin = skin; string t. Score = "<size='28'><color='navy'>Score : ##</color></size>"; string t. Height = "<size='28'><color='navy'>Height : ##</color></size>"; string t. Gift = "<size='28'><color='navy'>Gift : ##</color></size>"; t. Score = t. Score. Replace("##", "" + (int)score); t. Height = t. Height. Replace("##", "" + (int)max. Y); t. Gift = t. Gift. Replace("##", "" + gift. Cnt); int w = Screen. width / 2; int h = Screen. height / 2; GUI. Label(new Rect(20, 30, 200, 80), t. Score); GUI. Label(new Rect(w - 50, 30, 150, 80), t. Height); GUI. Label(new Rect(w * 1. 5 f, 30, 120, 80), t. Gift); if (!is. Dead) return;
7. 25 득점 및 게임 오버 표시 Screen. show. Cursor = true; // 커서 보이기 // 배경 음악 바꾸기 if (audio. clip != snd. Over) { audio. clip = snd. Over; audio. loop = false; audio. Play(); } if (GUI. Button(new Rect(w - 70, h - 50, 140, 60), "Play Game")) { Application. Load. Level("Main. Game"); } if (GUI. Button(new Rect(w - 70, h + 50, 140, 60), "Quit Game")) { Application. Quit(); } }
- Slides: 37