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

[Unity] #6. 게임 플레이 컨텐츠 만들기 : 미션 in Prop_Down/Up

by 로토마 2022. 3. 2.

자 이제 5번째 미션을 구현할 차례이다. 

이 미션은 유일하게 두 번 수행하는 미션으로 저번 Lab에서의 미션에 비해 간단히 구현할 수 있다.

 

팬 각도 조정하기)

Mission 5 UI

미션 5의 UI는 저렇게 구성했다.

목표는 빨간색 팬을 오른쪽의 핸들을 위 아래로 조작해 흰색 팬과 겹치도록 해서 파란색을 띄게끔하면 성공이다.

먼저 Hierachy를 살펴보면 아래와 같다.

Mission 5 구성

구성을 살펴보면 먼저 Handle과 Fix Rotate 이렇게 세 요소가 새롭게 추가된 것을 볼 수 있다.

Handle - 갈색의 핸들

Fix - 각도를 수정할 팬

Rotate - 랜덤으로 각도가 고정되어 있는 흰색 팬

또, Prop_Up과 Down 두곳에 미션을 넣어주었다. 

해당 Sprite를 넣어주고~ 위치도 위의 UI 처럼 위치되도록 고정해 주었다. 

 

script에서는

- Rotate, Fix의 각도를 랜덤으로 지정하는 기능

- Handle의 y값의 제한을 걸기

- Handle의 y값을 통해 Fix의 각도를 조정할 수 있는 기능

- Hanlde과 Fix의 각도가 해당 범주에 부합하면 Fix를 파란색으로 바꾸고 미션 종료하는 기능

이렇게 구현해주었다.

 

Mission 5 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
public class Mission5 : MonoBehaviour
{
    public Transform rotate, handle;
    public Color blue, red;
 
    Animator anim;
    PlayerCtrl playerCtrl_script;
    RectTransform rect_handle;
    MissionCtrl missionCtrl_script;
 
 
 
    bool isDrag, isPlay;
    float rand;
 
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        rect_handle = handle.GetComponent<RectTransform>();
        missionCtrl_script = FindObjectOfType<MissionCtrl>();
 
    }
 
    private void Update()
    {
        if (isPlay)
        {
            // 드래그 할 때
            if (isDrag)
            {
                handle.position = Input.mousePosition;
                rect_handle.anchoredPosition = new Vector2(184, Mathf.Clamp(rect_handle.anchoredPosition.y, -195195));
 
                // 드래그가 끝나면
                if (Input.GetMouseButtonUp(0))
                {
                    // 성공여부 체크
                    if(rect_handle.anchoredPosition.y > -5 && rect_handle.anchoredPosition.y < 5)
                    {
                        Invoke("MissionSuccess"0.2f);
                        isPlay = true;
                    }
 
                    isDrag = false;
 
                }
            }
            rotate.eulerAngles = new Vector3(0090 * rect_handle.anchoredPosition.y / 195);
 
            //색 변경
            if (rect_handle.anchoredPosition.y > -5 && rect_handle.anchoredPosition.y < 5)
            {
                rotate.GetComponent<Image>().color = blue;
            }
            else
            {
                rotate.GetComponent<Image>().color = red;
            }
        }  
 
    }
    //미션 시작
    public void MissionStart()
    {
        anim.SetBool("isUp"true);
        playerCtrl_script = FindObjectOfType<PlayerCtrl>();
        //초기화
        rand = 0;
 
        //랜덤
        rand = Random.Range(-195195);
 
        while(rand<=-10 && rand <= 10)
        {
            rand = Random.Range(-195195);
 
        }
        rect_handle.anchoredPosition = new Vector2(184, rand);
 
        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

 

코드는 위와 같이 짜주었고, 결과적으로 아래와 같이 잘 작동하는 것을 볼 수 있다.

 

Mission 5 플레이 영상