Unity/2D RPG

돌진 공격 패턴 by Unity

YunSeong 2021. 8. 28. 20:16
728x90
반응형

https://yunseong.tistory.com/entry/ERROR2D-RPG-game-%EA%B0%9C%EB%B0%9C-%EC%9D%BC%EC%A7%80-12-byUnity-%EB%B3%B4%EC%8A%A4-%EA%B3%B5%EA%B2%A9-%ED%8C%A8%ED%84%B4

 

[ERROR]2D RPG game 개발 일지 # 12 byUnity (보스 공격 패턴)

보스를 게임에서 구현할 때 여러가지의 공격 패턴들을 만들고 그 패턴들에 연계가 필요하다면 순서대로 그게 아니라면 무작위로 실행하도록 한다. - BossController c#파일 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1

yunseong.tistory.com

위 페이지에서 만든 보스의 패턴 중 두개를 만들것이다. 

 

 

위 동영상에서 돌진하는 부분을 구현했다. (뼈를 뿜는 부분은 다음 글에서 쓸 것이다.)

 

 

 

- 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
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
    void lookPlayer()
    {
        DIRECTION = (player.GetComponent<Transform>().position.x < transform.position.x ? -1 : 1); //player와 자신(보스)의 x좌표를 비교해서 적당한 상수를 DIRECTION에 저장한다.
        float scale = transform.localScale.z;
        transform.localScale = new Vector3(DIRECTION * -1 * scale, scale, scale); //DIRECTION변수를 이용해서 player쪽을 바라보도록한다.
    }
 
    IEnumerator rush()
    {
        animator.SetTrigger("ready"); //돌진을 준비하는 animation
        lookPlayer();//player 쪽을 보게한다.
        yield return new WaitForSeconds(1);
        animator.SetTrigger("rushing"); //돌진하는 animation
        bool isBroken = false;
        while (!isBroken)
        {
            yield return new WaitForSeconds(0.1f);
            if (speedx > rigid2D.velocity.x * DIRECTION) //AddForce를 이용해서 자연스럽게 움직이도록 하되 speedx보다 빠르지 않도록한다.
            {
                rigid2D.AddForce(transform.right * DIRECTION * 1000);
            }
 
            pos = new Vector2(transform.position.x + DIRECTION * 5, transform.position.y);
            boxSize = new Vector2(12);
            Collider2D[] collider2Ds = Physics2D.OverlapBoxAll(pos, boxSize, 0);//자신(보스) 앞쪽에 있는 모든 충돌체들을 저장한다.
            foreach (Collider2D collider in collider2Ds)
            {
                if (collider != this.GetComponent<CapsuleCollider2D>() && (collider.tag == "Ground")) //충돌체가 자신(보스)의 충돌체가 아니고 tag가 Ground일 때 멈추도록한다.
                {
                    rigid2D.velocity = new Vector2(0, rigid2D.velocity.y);
                    isBroken = true;
                    break;
                }
            }
        }
        animator.SetBool("stunned"true); //부딪혀서 스턴 당한 animation
        StartCoroutine("stunCounter"); //4초후에 stunned를 false로 설정하는 coroutine을 호출한다.
        yield return new WaitForSeconds(1.5f);
        this.glopGenerator_1.GetComponent<GlopGenerator>().summonGlop(); //Glop(몬스터를 소환한다.)
        this.glopGenerator_2.GetComponent<GlopGenerator>().summonGlop();
        this.glopGenerator_3.GetComponent<GlopGenerator>().summonGlop();
 
        nextPattern = EAT; // 연계를 위해서 
 
        yield return new WaitForSeconds(3);
 
        nextPatternPlay(); //다음 패턴을 실행한다.
    }
 
    IEnumerator eat()
    {
        animator.SetTrigger("ready");
        lookPlayer();
        yield return new WaitForSeconds(1);
        animator.SetTrigger("eating");
        this.isCanDamaged = true;
 
        int eatCount = 0;
 
        bool isBroken = false;
        while (!isBroken)
        {
            yield return new WaitForSeconds(0.1f);
            if (speedx > rigid2D.velocity.x * DIRECTION)
            {
                rigid2D.AddForce(transform.right * DIRECTION * 1000);
            }
 
            pos = new Vector2(transform.position.x + DIRECTION * 5, transform.position.y);
            boxSize = new Vector2(12);
 
            Collider2D[] collider2Ds = Physics2D.OverlapBoxAll(pos, boxSize, 0);
            foreach (Collider2D collider in collider2Ds)
            {
                if (collider.gameObject != gameObject && (collider.tag == "Ground"))
                {
                    isBroken = true;
                    break;
                }
                else if (collider.gameObject != gameObject && collider.tag == "Villain"//tag가 villain일 때 즉 아까 소환한 Glop(몬스터)일 때 그것을 파괴하고 그 숫자를 센다.
                {
                    Destroy(collider.gameObject);
                    eatCount += 1;
                }
            }
        }
        animator.SetBool("stunned"true);
        StartCoroutine("stunCounter");
 
        this.isCanDamaged = false;
        
        amountOfBone = eatCount * 3//먹은(파괴한) Glop(몬스터)의 수에 따라서 뼈의 수를 정한다.
 
        if (amountOfBone == 0//뼈가 없을 때는 랜텀으로 다음 패턴을 정한다.
        {
            if (0 == Random.Range(02)) nextPattern = RUSH;
            else nextPattern = JUMP;
        }
        else //뼈가 있을 때는 연계를 위해서 강제로 다음 패턴을 정한다.
        {
            nextPattern = SPIT;
        }
 
        yield return new WaitForSeconds(3);
 
        nextPatternPlay();
    }
 
    IEnumerator stunCounter() //위에서 호출되는 함수로 4초가 지난 후에 stunned를 false로 설정한다.
    {
        yield return new WaitForSeconds(4);
        animator.SetBool("stunned"false);
    }
cs

 

 

- GlopGenerator (empty Object에 넣어준다. 위치는 적당하게 맞춘다.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class GlopGenerator : MonoBehaviour
{
    public GameObject groundGlop;
 
    public void summonGlop()
    {
        GameObject glop = Instantiate(this.groundGlop, new Vector3(transform.position.x, transform.position.y, 0), transform.rotation) as GameObject;
    }
}
 
cs

 

 

 

 

 

 

728x90
반응형

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

점프 공격 패턴 by Unity  (0) 2021.08.28
다중 투사체 패턴 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