Unity/2D RPG

보스 공격 패턴 by Unity

YunSeong 2021. 8. 28. 19:47
728x90
반응형

보스를 게임에서 구현할 때 여러가지의 공격 패턴들을 만들고

그 패턴들에 연계가 필요하다면 순서대로

그게 아니라면 무작위로 실행하도록 한다.

 

- BossController  c#파일

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class gatekeeper : MonoBehaviour
{
    private int nextPattern = 0;
 
    private static readonly int NONE = 0;
    private static readonly int RUSH = 1;
    private static readonly int EAT = 2;
    private static readonly int JUMP = 3;
    private static readonly int SPIT = 4;
 
    IEnumerator rush() 
    {
            ...
        nextPatternPlay()
    }
    
    IEnumerator eat()
    {
            ...
        nextPatternPlay()
    }
 
    IEnumerator jump()
    {
            ...
        nextPatternPlay()
    }
 
    IEnumerator spit()
    {
            ...
        nextPatternPlay()
    }
 
    void Start()
    {
        StartCoroutine("rush");
    }
 
    void nextPatternPlay()
    {
        switch (nextPattern)
        {
            case 1:
                StartCoroutine(rush());
                break;
            case 2:
                StartCoroutine(eat());
                break;
            case 3:
                StartCoroutine(jump());
                break;
            case 4:
                StartCoroutine(spit());
                break;
        }
    }
}
 
cs

7~13 - nextPattern에 9~13에서 정의해둔 "JUMP"등의 상수들을 적절히 저장해서 다음 실행할 패턴을 알 수 있도록 했다. (상수라는 표현이 맞는지는 모르겠다.)

15~37 - 각각의 coroutine을 활용해서 각각의 패턴을 실행할 수 있도록 했다. nextPatternPlay()함수를 호출하기 전에 nextPattern변수에다가 적절한 상수를 저장해줘야한다.

39~42 - 맨처음 실행되야하는 coroutine을 호출한다.

44~61 - nextPattern변수저장된 상수를 통해서 다음 실행되야하는 coroutineswitch문을 통해서 호출한다.

 

 

 

728x90
반응형

'Unity > 2D RPG' 카테고리의 다른 글

다중 투사체 패턴 by Unity  (0) 2021.08.28
돌진 공격 패턴 by Unity  (0) 2021.08.28
키 배치 설정 by Unity  (0) 2021.08.18
Canvas에서 메뉴 by Unity  (0) 2021.08.18
캐릭터 나눠지고 합쳐지게 by Unity  (0) 2021.08.05