Unity - 2D 종스크롤 슈팅 : 완성하기

TXMAY·2023년 10월 19일
0

Unity 튜토리얼

목록 보기
33/33
post-thumbnail

이번 강좌에서 2D 종스크롤 슈팅 게임을 완성한다.
원래 강좌에서는 모바일 슈팅 게임으로 만들지만 내가 아직 모바일 게임을 만들 계획이 없기도 하고 학교에서 핸드폰을 사용하기도 어려워 그 파트는 생략할 것이다.


플레이어 무적 시간

'Player.cs'에 다음과 같이 코드를 추가한다.

...

public bool isRespawnTime;
...

SpriteRenderer spriteRenderer;

void Awake()
{
    ...
    
    spriteRenderer = GetComponent<SpriteRenderer>();
}

void OnEnable()
{
    Unbeatable();
    Invoke("Unbeatable", 3);
}

void Unbeatable()
{
    isRespawnTime = !isRespawnTime;

    if (isRespawnTime)
    {
        spriteRenderer.color = new Color(1, 1, 1, 0.5f);

        for (int index = 0; index < followers.Length; index++)
        {
            followers[index].GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0.5f);
        }
    }
    else
    {
        spriteRenderer.color = new Color(1, 1, 1, 1);
        for (int index = 0; index < followers.Length; index++)
        {
            followers[index].GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 1);
        }
    }

}
...


void OnTriggerEnter2D(Collider2D collision)
{
    ...
    
    else if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "EnemyBullet")
    {
        if (isRespawnTime)
        {
            return;
        }
		...
        
    }
    ...
    
}

폭발 효과

플레이어나 적이 죽을 때 폭발 효과를 넣어 보겠다.
'Explosion' 스프라이트를 배치하고 애니메이션을 만든다.
그리고 Animator에서 Create - Empty로 빈 State를 생성하고 Set as Layer Default State를 누른다.
그런 다음 Any State -> Explosion -> Idle로 연결한다.
그리고 'Explosion.cs'를 생성한 후 다음과 같이 코드를 작성한다.

using UnityEngine;

public class Explosion : MonoBehaviour
{
    Animator anim;

    void Awake()
    {
        anim = GetComponent<Animator>();
    }
    
    void OnEnable()
    {
        Invoke("Disable", 2f);
    }

    void Disable()
    {
        gameObject.SetActive(false);
    }

    public void StartExplosion(string target)
    {
        anim.SetTrigger("OnExplosion");

        switch (target)
        {
            case "S":
                transform.localScale = Vector3.one * 0.7f;
                break;
            case "M":
            case "P":
                transform.localScale = Vector3.one * 1f;
                break;
            case "L":
                transform.localScale = Vector3.one * 2f;
                break;
            case "B":
                transform.localScale = Vector3.one * 3f;
                break;
        }
    }
}

그런 다음 프리펩을 만들고 오브젝트 풀링에 등록한다.

...

public GameObject explosionPrefab;
...

GameObject[] explosion;
...

void Awake()
{
	...
    
    explosion = new GameObject[20];
	...
    
}

void Generate()
{
    ...
    
    for (int index = 0; index < explosion.Length; index++)
    {
        explosion[index] = Instantiate(explosionPrefab);
        explosion[index].SetActive(false);
    }
}

public GameObject MakeObj(string type)
{
    switch (type)
    {
        ...
        
        case "Explosion":
            targetPool = explosion;
            break;
    }
    ...
    
}

public GameObject[] GetPool(string type)
{
    switch (type)
    {
        ...
        
        case "Explosion":
            targetPool = explosion;
            break;
    }
    ...
    
}

그리고 'Player.cs'와 'Enemy.cs'에서 각각 죽을 때 애니메이션이 재생되도록 'GameManager.cs'에 다음과 같이 코드를 추가한다.

// GameManager.cs
...

 void SpawnEnemy()
 {
     ...
     
     enemyLogic.gameManager = this;
     ...
     
}
...

// Player.cs
...

void OnTriggerEnter2D(Collider2D collision)
{
    ...
    
    else if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "EnemyBullet")
    {
        ...
        
        gameManager.CallExplosion(transform.position, "P");
		...
        
    }
}

// Enemy.cs
...

public GameManager gameManager;
...

public void OnHit(int dmg)
{
    ...
    
    if (health <= 0)
    {
        ...
        
        gameManager.CallExplosion(transform.position, enemyName);
    }
}

이렇게 두 번째 강좌도 모두 끝이 났다.
이제 이 내용을 참고해서 프로젝트를 만들어 봐야겠다.

profile
게임 개발 공부하는 고양이

0개의 댓글