이번 강좌는 아이템과 필살기에 관한 강좌다.
'Boom', 'Power', 'Coin' 스프라이트를 배치한다.
그리고 Collider와 Rigidbody를 추가하고 Is Trigger 활성화, Gravity Scale - 0으로 설정한다.
'Item.cs' 파일을 생성한 후, 다음과 같이 코드를 작성한다.
using UnityEngine;
public class Item : MonoBehaviour
{
public string type;
Rigidbody2D rigid;
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
rigid.velocity = Vector2.down * 3;
}
}
그리고 애니메이션과 Item 태그를 추가한다.
'Player.cs'에 다음과 같이 코드를 추가하고 수정한다.
...
public int power;
public int maxPower;
...
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Border")
{
...
}
...
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;
...
}
}
}
'Boom' 스프라이트를 배치하고 Pixel Per Unit을 15로 설정한다.
그리고 화면의 플레이어나 아이템을 가리지 않게 하기 위해 Sprite Renderer에 Order in Layer를 -1로, Grid는 -2로 설정한다.
그런 다음 애니메이션을 추가하고 Speed를 3으로 설정한다.
그리고 다음과 같이 코드들을 추가하고 수정한다.
// Player.cs
...
public GameObject boomEffect;
public int maxBoom;
public int boom;
...
void Update()
{
...
Boom();
...
}
...
void Boom()
{
if (!Input.GetButton("Fire2"))
{
return;
}
if (isBoomTime)
{
return;
}
if (boom == 0)
{
return;
}
boom--;
isBoomTime = true;
boomEffect.SetActive(true);
Invoke("OffBoomEffect", 4f);
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
for (int index = 0; index < enemies.Length; index++)
{
Enemy enemyLogic = enemies[index].GetComponent<Enemy>();
enemyLogic.OnHit(1000);
}
GameObject[] bullets = GameObject.FindGameObjectsWithTag("EnemyBullet");
for (int index = 0; index < bullets.Length; index++)
{
Destroy(enemies[index]);
}
}
...
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Border")
{
...
}
...
else if (collision.gameObject.tag == "Item")
{
...
switch (item.type)
{
...
case "Boom":
if (boom == maxBoom)
{
score += 500;
}
else
{
boom++;
}
break;
}
Destroy(collision.gameObject);
}
}
void OffBoomEffect()
{
boomEffect.SetActive(false);
isBoomTime = false;
}
...
// Enemy.cs
...
public void OnHit(int dmg)
...
FindGameObjectsWithTag(tag) : 태그로 씬의 모든 오브젝트 추출
그리고 라이프처럼 필살기 사용 횟수를 표시하기 위해 UI를 만들고 다음과 같이 코드들을 추가한다.
// GameManager.cs
...
public Image[] boomImage;
...
public void UpdateBoomIcon(int boom)
{
for (int index = 0; index < 3; index++)
{
boomImage[index].color = new Color(1, 1, 1, 0);
}
for (int index = 0; index < boom; index++)
{
boomImage[index].color = new Color(1, 1, 1, 1);
}
}
// Player.cs
...
void Boom()
{
...
manager.UpdateBoomIcon(boom);
}
void OnTriggerEnter2D(Collider2D collision)
{
...
else if (collision.gameObject.tag == "Item")
{
...
case "Boom":
...
else
{
...
manager.UpdateBoomIcon(boom);
}
...
}
...
}
}
그리고 필살기 아이콘의 투명도를 0으로 설정한다. (Color에 A(Alpha) 부분을 0으로 줄이면 된다)
'Enemy.cs'에 다음과 같이 코드를 추가한다.
...
public GameObject itemCoin;
public GameObject itemPower;
public GameObject itemBoom;
...
public void OnHit(int dmg)
{
if (health <= 0)
{
return;
}
...
if (health <= 0)
{
...
int ran = Random.Range(0, 10);
if (ran < 3)
{
Debug.Log("Nope");
}
else if (ran < 6)
{
Instantiate(itemCoin, transform.position, itemCoin.transform.rotation);
}
else if (ran < 8)
{
Instantiate(itemPower, transform.position, itemPower.transform.rotation);
}
else if (ran < 10)
{
Instantiate(itemBoom, transform.position, itemBoom.transform.rotation);
}
...
}
}
아이템을 모두 프리펩으로 만든 후, 적 프리펩의 변수에 넣는다.
아이템까지 추가하니 점점 완성에 가까워지는 것 같다.