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

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

by 로토마 2022. 3. 4.

이제 드디어 마지막 미션 6을 구현하면 미션 부분은 대부분 마무리!!

이번 미션은 지금까지의 미션 중에서 가장 오류가 많이 발생해... 고전했다..

지금도 구현엔 성공했으나,,, scale을 조정해도 마우스에 선이 잘 안달라붙는 버그는,,,, 여전하다..

혹시 이 해결법 아시는 분들은..!! 댓글로 알려주세요~~

 

그럼 바로 대망의 마지막 미션!!

전선 잇기) 

Mission 6 UI

전선 잇기 미션은 위의 UI에서 각 색깔에 맞추어 전선을 이으면 클리어!!

우선 이번에도 Hierachy창에서 구성을 살펴보면 아래와 같다.

Mission 6 Hierachy 구성

 위의 UI처럼 오른쪽 왼쪽 양쪽에 색을 표시해야하기 때문에, left와 right 두 요소를 추가해 각 색을 추가했주었다.

그리고 왼족에 line renderer을 통해 선을 그을 수 있도록 조작해 주었다.

line renderer inspector 창

이렇게 Inspector 창에서 설정 해 준다음에 본격적으로 script로 기능들을 구현했다.

- 마우스 위치를 따라 선이 그려지는 기능

- 랜덤으로 right 라인이 섞여 UI 출력되는 기능

- 모든 색깔이 잘 연결되었는지 확인하는 기능

 

Mission 6 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
public class Mission6 : MonoBehaviour
{
    public bool[] isColor = new bool[4];
    public RectTransform[] rights;
    public LineRenderer[] lines;
 
    Animator anim;
    PlayerCtrl playerCtrl_script;
    MissionCtrl missionCtrl_script;
 
 
    Vector2 clickPos;
    LineRenderer line;
    Color leftC, rightC;
 
 
    bool isDrag;
    float leftY, rightY;
 
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        missionCtrl_script = FindObjectOfType<MissionCtrl>();
 
    }
 
    private void Update()
    {
        
        
        // 드래그 할 때
        if (isDrag)
        {
 
            line.SetPosition(1new Vector3(Input.mousePosition.x - clickPos.x, Input.mousePosition.y - clickPos.y, -10));
            // 드래그가 끝나면
            if (Input.GetMouseButtonUp(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
                RaycastHit hit;
 
                //오른쪽 선에 다았다면
                if (Physics.Raycast(ray, out hit))
                {
                    GameObject rightLine = hit.transform.gameObject;
                    //오른쪽 선 y 값
                    rightY = rightLine.GetComponent<RectTransform>().anchoredPosition.y;
 
                    //오른쪽 선 색상
                    rightC = rightLine.GetComponent<Image>().color;
 
                    line.SetPosition(1new Vector3(500, rightY - leftY, -10));
 
                    // 색 비교
                    if(leftC == rightC)
                    {
                        switch(leftY)
                        {
                            case 225: isColor[0= truebreak;
                            case 75: isColor[1= truebreak;
                            case -75: isColor[2= truebreak;
                            case -225: isColor[3= truebreak;
                        }
                    }
                    else
                    {
                        switch (leftY)
                        {
                            case 225: isColor[0= falsebreak;
                            case 75: isColor[1= falsebreak;
                            case -75: isColor[2= falsebreak;
                            case -225: isColor[3= falsebreak;
                        }
                    }
                    //성공여부 체크
                    if(isColor[0&& isColor[1&& isColor[2&& isColor[3])
                    {
                        Invoke("MissionSuccess"0.2f);
                    }
                }
                //닿지 않았다면
                else
                {
                    line.SetPosition(1new Vector3(00-10));
                }
 
 
                isDrag = false;
 
            }
        }
           
        
 
    }
    //미션 시작
    public void MissionStart()
    {
        anim.SetBool("isUp"true);
        playerCtrl_script = FindObjectOfType<PlayerCtrl>();
       
        // 초기화
        for(int i = 0; i < 4; i++)
        {
            isColor[i] = false;
            lines[i].SetPosition(1new Vector3(00-10));
        }
 
        //랜덤
        for(int i = 0; i < rights.Length; i++)
        {
            Vector3 temp = rights[i].anchoredPosition;
 
            int rand = Random.Range(04);
            rights[i].anchoredPosition = rights[rand].anchoredPosition;
 
            rights[rand].anchoredPosition = temp;
        }
    }
 
    //엑스 버튼 누르면 호출
    public void ClickCancle()
    {
        anim.SetBool("isUp"false);
        playerCtrl_script.MissionEnd();
    }
 
    // 선 누르면 호출
    public void ClickLine(LineRenderer click)
    {
        clickPos = Input.mousePosition;
        line = click;
 
        //왼쪽 선 y값
        leftY = click.transform.parent.GetComponent<RectTransform>().anchoredPosition.y;
 
        // 왼쪽 선 색상
        leftC = click.transform.parent.GetComponent<Image>().color;
 
        isDrag = true;
    }
 
    //미션 성공하면 호출
    public void MissionSuccess()
    {
        ClickCancle();
        missionCtrl_script.MissionSuccess(GetComponent<CircleCollider2D>());
 
    }
 
}
 
cs

 

이렇게 미션 6까지 다 구현해 보았다. 

 

이렇게 미션 모두를 구현했지만, 이제 모든 미션이 다른 요소들과도 상호작용하면서 하나의 게임안에 포함시키기 위한 과정을 거쳐야 한다. 이 작업은 다음 블로그 MissionCtrl과 PlayerCtrl을 사용해 조작해보도록 하겠다!!