Unity/2D RPG

점프 공격 패턴 by Unity

YunSeong 2021. 8. 28. 20:44
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
    void lookPlayer() //player 쪽을 바라보게한다.
    {
        DIRECTION = (player.GetComponent<Transform>().position.x < transform.position.x ? -1 : 1);
        float scale = transform.localScale.z;
        transform.localScale = new Vector3(DIRECTION * -1 * scale, scale, scale);
    }
 
    IEnumerator jump()
    {
        lookPlayer();
        animator.SetTrigger("jumpReady"); //점프를 준비하는 animation
 
        yield return new WaitForSeconds(1);
 
        rigid2D.velocity = new Vector2(0100);
 
        animator.SetTrigger("jumpUp"); //위쪽으로 점프하는 animation
 
        GameObject shadow;
 
        while (true)
        {
            if (transform.position.y >= 100//너무 윗쪽으로 올라가면 오래걸리기 때문에 100까지 올라가면 떨어지도록 한다.
            {
                animator.SetTrigger("jumpDown"); //떨어지는 animation
                rigid2D.velocity = new Vector2(0-1);
                break;
            }
            yield return new WaitForSeconds(0.1f);
        }
 
        float playerXPosition = player.GetComponent<Transform>().position.x; //플레이어의 위치로 떨어져서 공격하기 위해 x좌표를 받아온다.
        if (playerXPosition > 5.5f) // 플레이어가 맵의 끝에 있으면 자신(보스)이 맵 밖으로 나갈 수도 있기 때문에 조정해준다.
        {
            transform.position = new Vector3(5.5f, 99, transform.position.z);
        }
        else if (playerXPosition < -6)
        {
            transform.position = new Vector3(-699, transform.position.z);
        }
        else
        {
            transform.position = new Vector3(player.GetComponent<Transform>().position.x, 99, transform.position.z);
        }
 
        shadow = Instantiate(shadowPrefab, new Vector3(transform.position.x, -5-3), Quaternion.Euler(0f, 20f, 0f)) as GameObject; //그림자를 소환한다.
        shadow.GetComponent<Transform>().localScale = new Vector3(000);
 
        while (true)
        {
            float scale = maxScale *(1 - ((transform.position.y - (바닥 y좌표)) / (100 + (바닥 y좌표)))); //떨어지는 자신(보스)의 y좌표에 따라서 그림자의 크기를 설정한다.
            shadow.GetComponent<Transform>().localScale = new Vector3(scale, scale, scale);
 
            if (rigid2D.velocity.y == 0//자신(보스)이 바닥에 닿으면 그림자를 없애준다.
            {
                Destroy(shadow); 
                animator.SetTrigger("jumpEnd"); //착지하는 animation
                break;
            }
            yield return new WaitForSeconds(0.1f);
        }
 
        if (0 == Random.Range(02)) nextPattern = RUSH; //랜덤으로 다음 패턴을 정한다.
        else nextPattern = JUMP;
 
        yield return new WaitForSeconds(3);
 
        nextPatternPlay();
    }
cs

 

728x90
반응형

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

보스 등장 인트로 by Unity  (0) 2021.11.14
다중 투사체 패턴 by Unity  (0) 2021.08.28
돌진 공격 패턴 by Unity  (0) 2021.08.28
보스 공격 패턴 by Unity  (0) 2021.08.28
키 배치 설정 by Unity  (0) 2021.08.18