유니티/유니티 프로젝트 (완성)

미로에서 동전 먹기 (유니티)

xortl98 2022. 4. 20. 13:17
728x90

 개요

간단한 사이트 프로젝트로 안드로이드에서 동작할 미로에서 동전먹기 게임을 만들어보았다.

 

 활용한 에셋 출처 

가상 조이스틱은 베르님의 유튜브를 보고 참고했다.

https://www.youtube.com/watch?v=MZHESbKQjpo 

그리고 미로는 에셋스토어에 있는 Maze Generator를 이용했다.

https://assetstore.unity.com/packages/tools/modeling/maze-generator-38689

 

Maze Generator | 모델링 | Unity Asset Store

Get the Maze Generator package from styanton and speed up your game development process. Find this & other 모델링 options on the Unity Asset Store.

assetstore.unity.com

 코드 

코드는 총 3개를 구현해주었다. 

 

맵에서 얼마나 코인이 있는지 확인 

더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class CoinCheck : MonoBehaviour
{
    [SerializeField]
    private GameObject gameClearPannel; //게임 클리어시 나오는 패널 

    public static CoinCheck Instance;   //싱글톤
    public int CoinCount = 0;   //전체 코인 개수 

    [SerializeField]
    private AudioSource CoinSound;

    [SerializeField]
    Text Coin_Count_TxT;

    private void Awake()
    {
        Instance = this;
    }
    void Start()
    {
        Invoke("Find_CoinCount", 0.1f);     
    }

    void Find_CoinCount()
    {
        for (int i = 0; i < transform.childCount; i++)
        {
            if (transform.GetChild(i).gameObject.name == "CoinPrefab(Clone)")
            {
                CoinCount++;
            }
        }

        Coin_Count_TxT.text = CoinCount.ToString();
        
        //Debug.Log(CoinCount);
    }

    //싱글톤으로 코인 스크립트에서 코인 먹을때 쓰임 
    public void Destroy_Coin()
    {
        CoinSound.Play();
        CoinCount -= 1;
        Coin_Count_TxT.text = CoinCount.ToString();

        if (CoinCount == 0)
        {
            gameClearPannel.SetActive(true);
        }

    }

    //조이스틱은 해당 패널에 막혀서 어차피 움직임이 불가능함 
    //만일 Restart버튼 누르면 씬 재시작 
    public void GameComplte_ReStartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}



/* 설명
자식 개체 개수를 먼저 받아오고 자식 개체를 직접 일일이 확인해서 CoinPrefab(Clone)이면 
하나씩 갯수를 더해줘서 총 코인 개수가 몇개인지 확인 
 */

코인 먹었을 시 처리 

더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GetCoin : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        //Debug.Log("누군가 들어왔다." + other.tag); 
        if(other.tag=="Player")
        {
            //CoinCount_TxT.text = (Coincount - 1).ToString();
            CoinCheck.Instance.Destroy_Coin();
            Destroy(gameObject);
        }
    }
}
/*
만일 코인이 통과하면 코인 사라지고 남은 코인 개수가 -1 됨  
 */

게임 클리어

더보기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Clear_Game : MonoBehaviour
{
    [SerializeField]
    private GameObject gameClearPannel;
    public void GameComplte_ReStartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

 

플레이 영상 

해당 영상은 플레이 영상입니다. 

 

 

 

+ 22.04.22 몇가지 기능을 더 넣어서 맛있게 완성하였다. 

 

넣은 기능은 미니맵 + 클리어 시간 보여주기 + 화면 스크롤 조이스틱이다.