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

[Unity] #9. 게임 플레이 컨텐츠 만들기 : 킬 퀘스트 구현하기

by 로토마 2022. 3. 4.

이제 마지막으로 킬 퀘스트를 구현해야한다.

킬 퀘스트에서는 지정한 10가지 위치 중 랜덤으로 5곳에 생성되는 npc를 킬하는 것이 목표이다.

킬 퀘스트

필요한 기능)

- USE 버튼 -> Kill 버튼으로 바꾸기

- 쿨타임 5초 주기

- 미션 비활성화

- npc와 위치 랜덤 스폰

- npc kill 할 때 플레이 할 애니메이션 제작

- npc kill count

- npc 모두 kill 했을 시 화면 전환

 

그럼 본격적으로 Kill의 구성부터 살펴보자.

Kill 구성

Hierachy를 살펴보면, 미션때와 마찬가지로 기본 맵이 Back으로 깔려 있고, 

조건에 맞춰 UI에 나타날 Animation과 text가 canvas에 있다.

또 랜덤으로 Spawn될 위치에 맞게 나타날 npc들을 SpawnPoint로 10군데 지정해두었다.

 

이제 NPC!!

NPC inspector 구성

NPC에는 멈춰 있을 때 생성될 이미지와, 죽은 후 나타날 이미지를 배열로 저장해주었다.

모두 1과 Dead로 되어있지만, 다 다른 색깔의 npc 이미지라는거!!

 

이제 본격적으로 애니메이션을 제작해보자.

먼저 animation 창으로 이동해주고~ 

각 이미지를 이어서 애니메이션을 만들면 된다.

Animation 창

 

자, 이제 마지막으로 script로 구현할 킬 퀘스트에 필요한 기능들에 대해 알아보자.

 

구현해야할 기능)

- USE 버튼 -> Kill 버튼으로 바꾸기

- 쿨타임 5초 주기

- 미션 비활성화

- npc와 위치 랜덤 스폰

- 애니메이션 플레이 조건 

- npc kill count

- npc 모두 kill 했을 시 화면 전환

이 작업을 각각 NPCCtrl과 KillCtrl script에서 구현하면 된다.

 

NPCCtrl 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class NPCCtrl : MonoBehaviour
{
    public Sprite[] idles, deads;
 
    SpriteRenderer sr;
 
    int rand;
 
    void Start()
    {
        sr = GetComponent<SpriteRenderer>();
 
        rand = Random.Range(05);
        sr.sprite = idles[rand];
    }
 
    // 죽은 이미지
    public void Dead()
    {
        sr.sprite = deads[rand];
 
        sr.sortingOrder = -1;
    }
    
}
 
cs

NPC script에서는 위치와, 이미지를 저장하고, Kill 했을 시 Dead 이미지로 바뀌는 작업을 해주었다.

 

KillCtrl 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class KillCtrl : MonoBehaviour
{
    public Transform[] spawnPoints;
    public GameObject kill_anim, text_anim, mainView ;
 
    List<int> number = new List<int>();
 
    int count;
 
    //초기화
 
    public void KillReset()
    {
        kill_anim.SetActive(false);
        text_anim.SetActive(false);
 
        number.Clear();
 
        for(int i = 0;i< spawnPoints.Length; i++)
        {
            if(spawnPoints[i].childCount != 0)
            {
                Destroy(spawnPoints[i].GetChild(0).gameObject);
            }
        }
 
        NPCSpawn();
    }
 
    //NPC 스폰
    public void NPCSpawn()
    {
        int rand = Random.Range(010);
 
        for(int i = 0; i < 5;)
        {
            // 중복되었다면
            if (number.Contains(rand))
            {
                rand = Random.Range(010);
            }
            // 중복되지 않았다면
            else
            {
                number.Add(rand);
                i++;
            }
        }
 
        //스폰
        for (int i = 0; i < number.Count; i++)
        {
            Instantiate(Resources.Load("NPC"), spawnPoints[number[i]]);
        }
    }
 
    // 킬 하면 호출
    public void Kill()
    {
        count++;
 
        if (count == 5)
        {
            text_anim.SetActive(true);
            Invoke("Change", 1f);
        }
    }
 
    // 화면 전환
    public void Change()
    {
        mainView.SetActive(true);
        gameObject.SetActive(false);
 
        //캐릭터 삭제
        FindObjectOfType<PlayerCtrl>().DestroyPlayer();
    }
}
 
cs

KillCtrl script에서는 랜덤으로 npc 이미지와 위치를 배치하고,

Kill animation을 재생, 그리고 모든 NPC들을 Kill 했을 때 text가 뜨고 화면이 전환되게끔 해 주었다.

 

이렇게 Kill 섹션도 무사히 마무리했다!!

Kill 플레이 영상