[Unity] 종 스크롤 슈팅 게임 6 (Item)

펑크린·2021년 9월 13일

Unity

목록 보기
9/13

출처: 골드 메탈 님의 유튜브 콘텐츠 클론 코딩

00. Prefab 준비

  • 제공된 sprite들을 프리펩으로 만든다.
  • Box Collider2D, Rigidbody2d, animator, script를 부착한다.

  • 위와같이 설정을 하고 프리펩으로 만든다.

01. Item 로직

아이템 획득

  • 플레이어와 부딪치면 획득한다.
  • power를 획득하면 플레이어의 power가 1증가한다.
  • boom을 획득하면 UI상에 boom icon이 갱신된다.
  • coin이나 다른 아이템을 한도를 초과하여 획득하면 점수로 치환된다.

Player Script

  • 아이템과 부딪치면 아이템의 타입에 따라 이벤트가 달라진다.
  • 코인을 먹으면 단순하게 스코어가 증가하고 파워를 먹으면 power가 1씩 증가하면 maxPower까지 증가한다.
  • Boom을 먹으면 Boom Icon이 하나씩 생긴다.
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
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Border")
        {
            code
        }
        else if(collision.gameObject.tag == "EnemyBullet" || collision.gameObject.tag == "Enemy")
        {
 
            code
        }
        else if(collision.gameObject.tag == "Item")
        {
            Item item = collision.gameObject.GetComponent<Item>();
            switch (item.type)
            {
                case "Coin":
                    score += 1000;
                    break;
                case "Power":
                    if(power == maxPower)
                    {
                        score += 500;
                    }
                    else
                    {
                        power++;
                    }
                    
                    break;
                case "Boom":
                    if(boom == 3)
                    {
                        score += 500;
                    }
                    else
                    {
                        boom++;
                    }
                    
                    GameManager gm = gameManager.GetComponent<GameManager>();
                    gm.UpdateBoomIcon(boom);
                    break;
            }
            Destroy(collision.gameObject);
        }
    }
cs

Boom 발사

  • 마우스 오른쪽 버튼을 누르면 Boom이 발사되도록 한다.
  • Boom은 적에게 큰 데미지를 주고 탄환을 제거하는 효과를 가진다.
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
    void Boom()
    {
        if (!Input.GetButton("Fire2"))
            return;
 
        if (isBoomTime || boom <= 0)
            return;
 
        isBoomTime = true;
 
        boom--;
        GameManager gm = gameManager.GetComponent<GameManager>();
        gm.UpdateBoomIcon(boom);
 
        boomEffect.SetActive(true);
        Invoke("OffBoomEffect", 3f);
        GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
        for (int i = 0; i < enemies.Length; i++)
        {
            Enemy enemyLogic = enemies[i].GetComponent<Enemy>();
            enemyLogic.OnHit(1000);
        }
 
        GameObject[] bullets = GameObject.FindGameObjectsWithTag("EnemyBullet");
        for (int i = 0; i < enemies.Length; i++)
        {
            Destroy(bullets[i]);
        }
 
 
    }
cs

Item Drop

  • 적을 제거하였을 때 일정 확률로 Item이 Drop되도록 한다.
  • Enemy 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
    public void OnHit(int dmg)
    {
        if (isHit)
            return;
 
        health -= dmg;
        spriteRenderer.sprite = sprites[1];
        isHit = true;
        Invoke("ReturnSprite"0.1f);
        Invoke("ReturnHit"0.01f);
 
        if (health <= 0)
        {
            Player playerScore = player.GetComponent<Player>();
            playerScore.score += enemyScore;
 
            Destroy(gameObject);
 
            int ran = Random.Range(010);
            if(ran < 3)
            {
                Debug.Log("No item");
            }
            else if(ran < 5)
            {
                Instantiate(itemCoin, transform.position, itemCoin.transform.rotation);
            }
            else if(ran < 7)
            {
                Instantiate(itemBoom, transform.position, itemBoom.transform.rotation);
            }
            else if(ran < 10)
            {
                Instantiate(itemPower, transform.position, itemPower.transform.rotation);
            }
        }
    }
cs

02. 결과

profile
코 익 인 간

0개의 댓글