[2D Sparta-Unity] 3์ฃผ์ฐจ Dog VS Cat โญโญ

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , ,




3์ฃผ์ฐจ Dog VS Cat

๊ณ ์–‘์ด์—๊ฒŒ ๋จน์ด์ฃผ๋Š” ๊ฒŒ์ž„ ์—ฌ๋Ÿฌ์”ฌ, ์ฒด๋ ฅ๋ฐ”, ๋‚œ์ด๋„

3์ฃผ์ฐจ Dog VS Cat






1. ๋ฐฐ๊ฒฝ, ์บ๋ฆญํ„ฐ ๊ตฌ์„ฑ

  • ์—ฌ๋Ÿฌ ์”ฌ
  • ๋‚œ์ด๋„
  • ์นด๋ฉ”๋ผ ์‚ฌ์ด์ฆˆ 25๋กœ ์ž‘์—…, ๋ฐฐ๊ฒฝ
  • ์ด๋ฏธ์ง€ ๋‹ค์šด๋ฐ›๊ณ  ์ €์žฅ








2. ๋‘๊ฐœ ์”ฌ

  • Main, Start Scene
  • ์”ฌ ๋„˜๊ธฐ๊ธฐ (์”ฌ ๋ฆฌ๋กœ๋“œ) SceneManager.LoadScene(โ€œMainSceneโ€);
  • image

์Šคํƒ€ํŠธ ๋ฒ„ํŠผ(์”ฌ๋„˜๊ธฐ๊ธฐ)

๋ฒ„ํŠผ์€ 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 ํ™•์ธ
  • image

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

์ฐธ๊ณ  : ์œ ๋‹ˆํ‹ฐ TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ