Limetime's TimeLine
article thumbnail
반응형

유니티를 공부하기 위한 첫걸음으로 이 동영상을 택했다.

구글링 해보니 Flappy Bird라는 게임은 널리 알려진 게임이었고 유니티의 기초적인 시스템을 이해하는데 좋은 기회인 것 같았다.

많은 Flappy Bird 게임이 있었지마 조코딩의 영상을 참고했다.

1. 요약.

1.1.

  • Bird : BirdJump.cs

=> 클릭 시 점프, 충돌 시 GameOverScene으로 넘어감. & BestScore와 비교하여 현재 점수가 더 높으면 경신

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using UnityEngine.SceneManagement; public class BirdJump : MonoBehaviour { System.Random rand = new System.Random(); Rigidbody2D rb; public float jumpPower; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody2D>(); } // Update is called once per frame void Update() { // 마우스 왼쪽 버튼 눌렀을 때(0) if (Input.GetMouseButtonDown(0)) { // 새를 y축으로 3칸 이동. rb.velocity = Vector2.up * jumpPower; // Vector2 : (0,1), Vector3 : (x,y,z) } } private void OnCollisionEnter2D(Collision2D collision) { if(Score.score > Score.bestScore) { Score.bestScore = Score.score; } SceneManager.LoadScene("GameOverScene"); } }
  • PipeGenerator : MakePipe.cs

=> 랜덤으로 파이프의 위치를 임의로 생성 & FPS 관리

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MakePipe : MonoBehaviour { public GameObject pipe; float timer = 0; public float timeDiff; // Update is called once per frame void Update() { timer += Time.deltaTime; if (timer > timeDiff) { GameObject newpipe = Instantiate(pipe); newpipe.transform.position = new Vector3(6, Random.Range(-1.7f, 5.7f), 0); timer = 0; Destroy(newpipe, 10.0f); } } }
  • ScoreCanvas - Text : Score.cs

=> 점수를 텍스트로 보여줌.

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Score : MonoBehaviour { public static int score = 0; public static int bestScore = 0; // Start is called before the first frame update void Start() { score = 0; } // Update is called once per frame void Update() { GetComponent<Text>().text = score.ToString(); //str(score) } }
  • Pipe : Move.cs, ScoreUp.cs

=> Move.cs : 파이프가 왼쪽으로 이동. 그리고 FPS 관리

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Move : MonoBehaviour { public float speed; // Update is called once per frame void Update() { transform.position += Vector3.left*speed*Time.deltaTime; // (-1,0,0) } }

=> ScoreUp.cs : 파이프 사이에 트리거를 끼워놓고 충돌 시 점수 획득.

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScoreUp : MonoBehaviour { private void OnTriggerEnter2D(Collider2D other) { Score.score++; } }
  • Upside

=> 하늘 충돌 시 GameOverScene으로 넘어감.

 

1.2.

  • Canvas - Panel - Image
  • Canvas - Panel - Score : CurrentScore.cs

=> CurrentScore.cs : 현재 스코어를 텍스트로 표시

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CurrentScore : MonoBehaviour { // Start is called before the first frame update void Start() { GetComponent<Text>().text = "Score : " + Score.score.ToString(); } }
  • Canvas - Panel - BestScore : BestScore.cs

=> BestScore.cs : 경신된 스코어를 표시

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class BestScore : MonoBehaviour { // Start is called before the first frame update void Start() { GetComponent<Text>().text = "Best Score : " + Score.bestScore; } }
  • Canvas - Panel - Button

=> Replay.cs를 적용한 버튼 (리플레이 버튼)

  • Replay : Replay.cs

=> Replay.cs : PlayScene으로 넘어가게 함.

<cs />
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Replay : MonoBehaviour { public void ReplayGame() { SceneManager.LoadScene("PlayScene"); } }

 

2. ※ 참고

  • Trigger : 충돌감지, 진입하는 이벤트에 사용됨.
  • Collision : 충돌할 때 이벤트가 발생한다. 충돌 시 이벤트 처리
  • Time.deltaTime() : 기기마다 처리할 수 있는 FPS(Frame per Sec)가 다르기 때문에 기기 성능에 관계없이 동일한 FPS로 관리해줌.
반응형
profile

Limetime's TimeLine

@Limetime

포스팅이 좋았다면 "공감❤️" 또는 "구독👍🏻" 해주세요!