내일배움캠프

내일배움캠프 Unity - 사전캠프 (고양이 밥주기 게임)

달시_Dalsi 2025. 1. 9. 17:29
728x90

 

위에서부터 내려오는 여러 고양이들에게 먹이를 던져 쫒아내는 간단한 게임입니다.

5마리를 쫒을때마다 레벨이 증가하며 레벨별로 기믹이 존재합니다.

 

 

Cat 스크립트

using UnityEngine;

public class Cat : MonoBehaviour
{
    public float speed = 10f;

    public GameObject hungry_cat;
    public GameObject full_cat;

    public RectTransform front;

    float full = 5.0f;
    float energy = 0.0f;

    bool isFull = false;

    public int type = 0;  // 0 = normal, 1 = fat, 1 = pirate

    private void Start()
    {
        if(type == 0)
        {
            speed = 10f;
            full = 5.0f;
        }
        else if(type == 1)
        {
            speed = 7f;
            full = 10.0f;
        }
        else if (type == 2)
        {
            speed = 12f;
            full = 5.0f;
        }

        float x = Random.Range(-9.0f, 9.0f);
        float y = 30.0f;

        transform.position = new Vector2(x, y);
    }

    void Update()
    {
        if(transform.position.y <= -16.0f)
        {
            GameManager_feedCat.Instance.GameOver();
        }


        if (energy >= full)
        {
            if(transform.position.x > 0)
            {
                transform.position += Vector3.right * 5 * Time.deltaTime;
            }
            else
            {
                transform.position += Vector3.left * 5 * Time.deltaTime;
            }
            return;
        }
        transform.position += Vector3.down * speed * Time.deltaTime;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Food") )
        {
            if(energy < full)
            {
                energy += 1f;
                front.localScale = new Vector3(energy / full, 1f, 1f);
                Destroy(collision.gameObject);
                if( energy >= full)
                {
                    if(!isFull)
                    {
                        isFull = true;
                        hungry_cat.SetActive(false);
                        full_cat.SetActive(true);
                        Destroy(gameObject, 10f);
                        GameManager_feedCat.Instance.AddScore();
                    }
                }
            }
        }
    }
}

 

 

게임 매니저 스크립트

using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class GameManager_feedCat : MonoBehaviour
{
    public static GameManager_feedCat Instance;

    public GameObject normalCat;
    public GameObject fatCat;
    public GameObject pirateCat;
    public GameObject retryBtn;

    public RectTransform levelFront;
    public TextMeshProUGUI levelText;

    int level = 0;
    int score = 0;

    private void Awake()
    {
        if (Instance == null) { Instance = this; }

        Application.targetFrameRate = 60;
    }


    void Start()
    {
        Time.timeScale = 1f;
        InvokeRepeating("MakeCat", 0f, 1f);
    }

    public void GameOver()
    {
        retryBtn.SetActive(true);
        Time.timeScale = 0f;
    }

    void MakeCat()
    {
        Instantiate(normalCat);

        int tmp = Random.Range(0, 9);
        switch (level)
        {
            case 1:
                if (tmp < 2) { Instantiate(normalCat); }
                break;

            case 2:
                if (tmp < 5) { Instantiate(normalCat); }
                break;

            case 3:
                Instantiate(fatCat);
                break;

            case 4:
                Instantiate(pirateCat);
                break;

            default:
                break;
        }
    }

    public void AddScore()
    {
        score++;
        level = score / 5;
        levelText.text = level.ToString();
        levelFront.localScale = new Vector3((score - level * 5) / 5.0f, 1, 1);
    }
}

 

 

Dog 스크립트

using UnityEngine;

public class Dog : MonoBehaviour
{
    [SerializeField] GameObject prefabs_Food;


    void Start()
    {
        InvokeRepeating("CreateFood", 0f, 0.15f);
    }

    // Update is called once per frame
    void Update()
    {
        float x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;

        if(x > 8.5f)
        {
            x = 8.5f;
        }
        if(x < -8.5f)
        {
            x = -8.5f;
        }

        transform.position = new Vector2(x, transform.position.y);
    }

    void CreateFood()
    {
        Vector2 myVector2 = new Vector2(transform.position.x, transform.position.y + 2);
        Instantiate(prefabs_Food, myVector2, Quaternion.identity);
    }
}

 

 

StartButton 스크립트

using UnityEngine;
using UnityEngine.SceneManagement;

public class StartButton : MonoBehaviour
{

    public void StartGame()
    {
        SceneManager.LoadScene("feedCat");
    }
}

 

 

Food 스크립트

using UnityEngine;

public class Food : MonoBehaviour
{
    public float speed = 5f;

    // Update is called once per frame
    void Update()
    {
        transform.position += Vector3.up * speed * Time.deltaTime;

        if(transform.position.y > 27)
        {
            Destroy(gameObject);
        }
    }
}

Unity Play에 업로드된 제 게임을 아래 링크에서 플레이할 수 있습니다.

 

https://play.unity.com/en/games/cf984ac5-acd1-4181-a58a-695571c1e287/campfeedcat

 

camp_feedCat on Unity Play

protect from cats

play.unity.com

 

728x90