본문 바로가기
유니티/AmongUs 클론코딩 (30일 프로젝트)

[Unity] #4. 게임 플레이 컨텐츠 만들기 : 미션 in Cafeteria

by 로토마 2022. 3. 1.

지금부터는 미션 내용을 구현하고, USE button을 눌렀을 때 게임 화면으로 전환될 UI를 구현하면 된다.

그리고 마지막으로 게이지 바를 연동해 7개의 모든 미션이 끝나면 다시 메인 화면으로 나가게끔 해 줄것이다.

먼저 Cafetria에 위치할 두 가지 미션을 살펴보도록 하자~

 

야자수 카드 맞추기)

Mission1 구성

위처럼 UI안에 Background에 버튼을 7개 생성해준다.

그리고 벌집 모양의 Image를 넣어준 뒤, 축구공 모양처럼 위치를 지정해준다.

Mission1 UI

그리고 저런식으로 랜덤으로 색이 변하게 해,

모두 흰색을 맞추면 미션 성공이 되도록 코드를 작성했다.

 

Mission 1 script)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
public class Mission1 : MonoBehaviour
{
    public Color red;
    public Image[] images;
 
    Animator anim;
    PlayerCtrl playerCtrl_script;
    MissionCtrl missionCtrl_script;
 
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        missionCtrl_script = FindObjectOfType<MissionCtrl>();
    }
 
    //미션 시작
    public void MissionStart()
    {
        anim.SetBool("isUp"true);
        playerCtrl_script = FindObjectOfType<PlayerCtrl>();
 
        //초기화
        for (int i = 0; i < images.Length; i++)
        {
            images[i].color = Color.white;
        }
 
        //랜덤
        for (int i = 0; i < 4; i++)
        {
            int rand = Random.Range(07);
 
            images[rand].color = red;
        }
    }
 
    //엑스 버튼 누르면 호출
    public void ClickCancle()
    {
        anim.SetBool("isUp"false);
        playerCtrl_script.MissionEnd();
    }
 
    //육각형 버튼 누르면 호출
    public void ClickButton()
    {
        Image img = EventSystem.current.currentSelectedGameObject.GetComponent<Image>();
 
        // 하얀색
        if (img.color == Color.white)
 
        {
            // 빨간색으로
            img.color = red;
        }
        //빨간색
        else
        {
            //하얀색
            img.color = Color.white;
        }
 
        // 성공여부 체크
        int count = 0;
 
        for (int i = 0; i < images.Length; i++)
        {
            if (images[i].color == Color.white)
            {
                count++;
            }
        }
        if (count == images.Length)
        {
            //성공
            Invoke("MissionSuccess"0.2f);
        }
 
    }
 
    //미션 성공하면 호출
    public void MissionSuccess()
    {
        ClickCancle();
        missionCtrl_script.MissionSuccess(GetComponent<CircleCollider2D>());
    }
 
}
cs

 

그럼 바로 이어서 Mission 2를 살펴보도록 하자.

 

쓰레기 버리기)

Mission 2 구성

구성은 이렇다. 우선 Mission 1과 마찬가지로 background를 두고 저렇게 아래와 같이 위치해준다.

보이지는 않지만 bottom이 있어 handle이 내려가면 bottom이 없어지게끔해,

스폰된 모든 쓰레기가 다 버려지도록 하면 미션 성공이 되는 원리이다.

Mission2 UI

bottom은 collider로 설정해주었고, Trash는 위치가 랜덤으로 스폰 되어 각 10개정도씩 생성되게 해 주었다.

그 후 물리작용, 중력을 두어 Trash끼리 상호작용하게 만들어 좀 더 현실감을 주었다.

 

Mission2 script)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
public class Mission2 : MonoBehaviour
{
    public Transform trash, handle;
    public GameObject bottom;
    public Animator anim_shake;
    MissionCtrl missionCtrl_script;
 
 
    Animator anim;
    PlayerCtrl playerCtrl_script;
    RectTransform rect_handle;
    
 
    bool isDrag,isPlay;
    Vector2 originPos; // handle 원래 위치
 
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        rect_handle = handle.GetComponent<RectTransform>();
        originPos = rect_handle.anchoredPosition; // 원래 위치 저장
        missionCtrl_script = FindObjectOfType<MissionCtrl>();
 
    }
 
    private void Update()
    {
        if (isPlay)
        {
            // 드래그 할 때
            if (isDrag)
            {
                handle.position = Input.mousePosition;
                rect_handle.anchoredPosition = new Vector2(originPos.x, Mathf.Clamp(rect_handle.anchoredPosition.y, -135-47));
 
                anim_shake.enabled = true;
 
                // 드래그가 끝나면
                if (Input.GetMouseButtonUp(0))
                {
                    rect_handle.anchoredPosition = originPos;
                    isDrag = false;
                    anim_shake.enabled = false;
 
                }
            }
 
            //쓰레기 배출
            if (rect_handle.anchoredPosition.y <= -130)
            {
                bottom.SetActive(false);
            }
            else
            {
                bottom.SetActive(true);
            }
 
            for (int i = 0; i < trash.childCount; i++)
            {
                if (trash.GetChild(i).GetComponent<RectTransform>().anchoredPosition.y <= -600)
                {
                    Destroy(trash.GetChild(i).gameObject);
                }
            }
 
            // 성공 여부 체크
            if (trash.childCount == 0)
            {
                MissionSuccess();
                isPlay = false;
 
                rect_handle.anchoredPosition = originPos;
                isDrag = false;
                anim_shake.enabled = false;
            }
        }
       
    }
    //미션 시작
    public void MissionStart()
    {
        anim.SetBool("isUp"true);
        playerCtrl_script = FindObjectOfType<PlayerCtrl>();
 
        // 초기화
        for(int i = 0; i < trash.childCount; i++)
        {
            Destroy(trash.GetChild(i).gameObject);
        }
 
        // 쓰레기 스폰
        for (int i = 0; i < 10; i++)
        {
            // 사과
            GameObject trash4 = Instantiate(Resources.Load("Trash/Trash4"), trash) as GameObject;
            // 위치, 각도 랜덤으로 지정
            trash4.GetComponent<RectTransform>().anchoredPosition = new Vector2(Random.Range(-180180), Random.Range(-180180));
            trash4.GetComponent<RectTransform>().eulerAngles = new Vector3(00, Random.Range(0180));
 
            // 캔
            GameObject trash5 = Instantiate(Resources.Load("Trash/Trash5"), trash) as GameObject;
            // 위치, 각도 랜덤으로 지정
            trash5.GetComponent<RectTransform>().anchoredPosition = new Vector2(Random.Range(-180180), Random.Range(-180180));
            trash5.GetComponent<RectTransform>().eulerAngles = new Vector3(00, Random.Range(0180));
        }
 
        for(int i = 0; i < 3; i++)
        {
            // 병
            GameObject trash1 = Instantiate(Resources.Load("Trash/Trash1"), trash) as GameObject;
            // 위치, 각도 랜덤으로 지정
            trash1.GetComponent<RectTransform>().anchoredPosition = new Vector2(Random.Range(-180180), Random.Range(-180180));
            trash1.GetComponent<RectTransform>().eulerAngles = new Vector3(00, Random.Range(0180));
 
            // 생선
            GameObject trash2 = Instantiate(Resources.Load("Trash/Trash2"), trash) as GameObject;
            // 위치, 각도 랜덤으로 지정
            trash2.GetComponent<RectTransform>().anchoredPosition = new Vector2(Random.Range(-180180), Random.Range(-180180));
            trash2.GetComponent<RectTransform>().eulerAngles = new Vector3(00, Random.Range(0180));
 
            // 비닐
            GameObject trash3 = Instantiate(Resources.Load("Trash/Trash3"), trash) as GameObject;
            // 위치, 각도 랜덤으로 지정
            trash3.GetComponent<RectTransform>().anchoredPosition = new Vector2(Random.Range(-180180), Random.Range(-180180));
            trash3.GetComponent<RectTransform>().eulerAngles = new Vector3(00, Random.Range(0180));
 
        }
        isPlay = true;
 
    }
 
    //엑스 버튼 누르면 호출
    public void ClickCancle()
    {
        anim.SetBool("isUp"false);
        playerCtrl_script.MissionEnd();
    }
 
    // 손잡이 누르면 호출
    public void ClickHandle()
    {
        isDrag = true;
    }
 
    //미션 성공하면 호출
    public void MissionSuccess()
    {
        ClickCancle();
        missionCtrl_script.MissionSuccess(GetComponent<CircleCollider2D>());
 
    }
 
}
 
cs

 

이렇게 두 가지 미션을 구현했다.

작동시키면 아래 영상과 같다.

 

지금까지 구현한 미션 1, 2