[2D Sparta-Unity] 3์ฃผ์ฐจ Dog VS Cat โญโญ
์นดํ ๊ณ ๋ฆฌ: Sparta Unity
3์ฃผ์ฐจ Dog VS Cat
๊ณ ์์ด์๊ฒ ๋จน์ด์ฃผ๋ ๊ฒ์ ์ฌ๋ฌ์ฌ, ์ฒด๋ ฅ๋ฐ, ๋์ด๋
1. ๋ฐฐ๊ฒฝ, ์บ๋ฆญํฐ ๊ตฌ์ฑ
- ์ฌ๋ฌ ์ฌ
- ๋์ด๋
- ์นด๋ฉ๋ผ ์ฌ์ด์ฆ 25๋ก ์์ , ๋ฐฐ๊ฒฝ
- ์ด๋ฏธ์ง ๋ค์ด๋ฐ๊ณ ์ ์ฅ
2. ๋๊ฐ ์ฌ
- Main, Start Scene
- ์ฌ ๋๊ธฐ๊ธฐ (์ฌ ๋ฆฌ๋ก๋) SceneManager.LoadScene(โMainSceneโ);
์คํํธ ๋ฒํผ(์ฌ๋๊ธฐ๊ธฐ)
๋ฒํผ์ sprite ๊ฐ ์๋๋ผ UI ์ด๋ฏธ์ง์ ๋ถ์ฌ์ผ ์๋
startBtn.cs
startBtn.cs
using UnityEngine.SceneManagement;
using UnityEngine;
public class startBtn : MonoBehaviour
{
public void GameStart(){
SceneManager.LoadScene("MainScene");
}
}
- ์ฌ ๋ก๋
3. food ์ฝ๋ฉ
- ์ฌ๋ผ๊ฐ๋ rigidbody -> Kinematic
- collider is trigger ํ์ธ
food.cs
food.cs
using UnityEngine;
public class food : MonoBehaviour
{
void Update()
{
transform.position += new Vector3(0.0f, 0.5f, 0.0f);
if (transform.position.y > 26.0f)
{
Destroy(gameObject);
}
}
}
4. dog
- ๋ง์ฐ์ค ํฌ์ง์ ์ผ๋ก ์์ง์ด๊ธฐ
- Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
dog.cs
dog.cs
using UnityEngine;
public class dog : MonoBehaviour
{
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float x = mousePos.x;
if (x > 8.5f)
{
x = 8.5f;
}
if (x < -8.5f)
{
x = -8.5f;
}
transform.position = new Vector3(x, transform.position.y, 0);
}
}
5. cat
- Back : ํฐ์, Front : ๋นจ๊ฐ์ -> ์ฒด๋ ฅ๋ฐ ์์ฑ front pivot ์กฐ์
- pivot : ๋ณ๊ฒฝ๋ ๋ ๊ธฐ์ค์ 0 ~ 1
- find ์ฌ์ฉํ์ฌ front ์ ์ค์ผ์ผ ๋ณ๊ฒฝ
- gameObject.transform.Find(โhungry/Canvas/frontโ).transform.localScale = new Vector3(energy / full, 1.0f, 1.0f);
cat.cs
cat.cs
using UnityEngine;
public class cat : MonoBehaviour
{
float full = 5.0f;
float energy = 0.0f;
bool isFull = false;
public int type = 0; // cat๋ง๋ค type ํ์ธ
void Start()
{
float x = Random.Range(-8.5f, 8.5f);
float y = 30.0f;
transform.position = new Vector3(x, y, 0);
if (type == 1)
{
full = 10.0f;
}
}
void Update()
{
if (energy < full)
{
if (type == 0)
{
transform.position += new Vector3(0.0f, -0.05f, 0.0f);
}
else if (type == 1)
{
transform.position += new Vector3(0.0f, -0.03f, 0.0f);
}
else if (type == 2)
{
transform.position += new Vector3(0.0f, -0.1f, 0.0f);
}
if (transform.position.y < -16.0f)
{
GameManager.I.gameOver();
}
}
else
{
if (transform.position.x > 0)
{
transform.position += new Vector3(0.05f, 0.0f, 0.0f);
}
else
{
transform.position += new Vector3(-0.05f, 0.0f, 0.0f);
}
Destroy(gameObject, 3.0f);
}
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.gameObject.tag == "food")
{
if (energy < full)
{
energy += 1.0f;
Destroy(coll.gameObject);
//์ฒด๋ ฅ๋ฐ ๋นจ๊ฐ๋ถ๋ถ(front)์ localScale ์ ENERGY์ ๋์ผํ๊ฒ ์กฐ์
gameObject.transform.Find("hungry/Canvas/front").transform.localScale = new Vector3(energy / full, 1.0f, 1.0f);
//ํธ๋๋ฅผ ์ด 6๋ฒ ๋ง์์ผ ์คํ์ด ๋๋ ๊ฒ ๊ฐ์์ ์์ ํด ๋ณด์์ต๋๋ค.
if (energy == full && isFull == false)
{
GameManager.I.addCat();
isFull = true;
gameObject.transform.Find("hungry").gameObject.SetActive(false);
gameObject.transform.Find("full").gameObject.SetActive(true);
}
}
}
}
}
- ๊ณ ์์ด ํ์ ๋ณ๋ก ์ด์, full ๋ค๋ฅด๊ฒ ์ค์
6. GameManager
- GameManager๋? ๊ฒ์ ์ ์ฒด๋ฅผ ์กฐ์จํ๋ ์ค๋ธ์ ํธ!
- ์ ์ / ๋ค์ ์์ / 3๋ฒ ์งธ ๋ค์ ์์์ ๋ถ์คํฐ / ๊ด๊ณ ๋ณด๊ธฐ ๋ฑ
- InvokeRepeating (์ฐ์ด๋ด๊ธฐ)
GameManager.cs
GameManager.cs
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager I;
public GameObject food;
public GameObject dog;
public GameObject normalCat;
public GameObject fatCat;
public GameObject piratecat;
public GameObject retryBtn;
public Text levelText;
public GameObject levelFront;
int level = 0;
int cat = 0;
void Awake()
{
I = this;
}
void Start()
{
Time.timeScale = 1.0f;
InvokeRepeating("makeFood", 0.0f, 0.15f);
InvokeRepeating("makeCat", 0.0f, 1.0f);
}
void makeCat()
{
Instantiate(normalCat);
if (level == 1)
{
float p = Random.Range(0, 10);
if (p < 2) Instantiate(normalCat);
}
else if (level == 2)
{ // ์ข ๋ ๋ง์ด
float p = Random.Range(0, 10);
if (p < 5) Instantiate(normalCat);
}
else if (level == 3)
{ // fatcat ๋ฑ์ฅ
float p = Random.Range(0, 10);
if (p < 3) Instantiate(normalCat);
Instantiate(fatCat);
}
else if (level >= 4)
{ // fat,pirate cat ๋ฑ์ฅ
float p = Random.Range(0, 10);
if (p < 3) Instantiate(normalCat);
if (3 <= p && p < 7) Instantiate(fatCat);
if (7 <= p && p < 10) Instantiate(piratecat);
}
}
void makeFood(){
float x = dog.transform.position.x;
float y = dog.transform.position.y + 2.0f;
Instantiate(food, new Vector3(x,y,0), Quaternion.identity);
}
public void gameOver()
{
retryBtn.SetActive(true);
Time.timeScale = 0.0f;
}
public void addCat()
{
cat += 1;
level = cat / 5;
levelText.text = level.ToString();
levelFront.transform.localScale = new Vector3((cat - level * 5) / 5.0f, 1.0f, 1.0f);
}
}
- InvokeRepeating (โ๋ฉ์๋ ์ด๋ฆโ, ์๊ฐ ๋๋ ์ด, ๋ฐ๋ณต ์๊ฐ ๋จ์)๋ฉ์๋๋ฅผ x์ด ํ ์คํํ๊ณ , ๋งค y์ด๋ง๋ค ๋ฐ๋ณตํด์ ์คํํ๋ ๊ฒ์ ์๋ฏธ
- Instantiate( ์ด๋ฏธ ๋ง๋ค์ด์ง ๊ฒ์ ์ค๋ธ์ ํธ๋ฅผ ํ์ํ ๋๋ง๋ค ์ค์๊ฐ์ผ๋ก ๋ง๋ ๋ค๋ ์๋ฏธ)
- ๋์ด๋๋ณ๋ก ๋ค๋ฅด๊ฒ ์ค์
7. ๊ณ ์์ด ๋ํ๋ด๊ธฐ
- normalcat > Canvas render mode -> world space
- ๋๊ฐ์ ์ค๋ก HP๋ฐ ํํ
- Pivot ์ด๋๋ฅผ ๊ธฐ์ค์ผ๋ก ๋์ด๋๊ฒ ๋๊ฐ ex) 0.5 ์ค์๊ธฐ์ค hp๋ฐ๋ ๊ธฐ์ค 0
- ์ ๋๋ฉ์ด์ ๋ นํ๋ก ์ค์
8. ๊ฒ์ ๋๋ด๊ธฐ
์ ๊ฐ์๋ค๊ณผ ๊ฐ์ด Retry
9. ์ ๋ฆฌ
- 3์ฃผ์ฐจ์์ ๋์ด๋์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์ค์ ํ๋ ๊ธฐ๋ฅ์ด ์ด๋ป๊ฒ ๋ง๋ค์ด์ง๋์ง ์๊ฒ ๋์๋ค.
- ํ๋ฆฌํน ์ ์ฌ์ฉํ ๋ unpack๊ธฐ๋ฅ ํ์ฉ
- ์ฒด๋ ฅ๋ฐ 2๊ฐ์ ๋ฐ๋ก ํํ
1ํ 23/06/14
2ํ 23/10/26 ๋ณต์ต, ์์
[Unity] Dog VS Cat
๋๊ธ๋จ๊ธฐ๊ธฐ