코인 이이템 드롭
적기가 죽으면 감독 오브젝트에게 죽었는지 여부와 죽은 위치를 알린다.
감독 오브젝트가 죽었는지 여부와 위치를 판단하여 아이템을 생성한다.
생성된 코인 오브젝트가 플레이어와 충돌하면 점수가 오른다.

코인 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour
{
GameObject direcroy;
public int coinScore;
private void Start()
{
this.direcroy = GameObject.Find("Director");
}
void Update()
{
this.transform.Translate(Vector2.down * 4 * Time.deltaTime);
//화면밖으로 나가면
if (this.gameObject.transform.position.y <= -6.0f)
{
Destroy(this.gameObject);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
this.direcroy.GetComponent<Director>().score += coinScore;
Destroy(this.gameObject);
}
}
}
감독 스크립트
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Director : MonoBehaviour
{
Text scoreUi;
public int score;
public Vector3 enemyPos;
public bool isDie;
public GameObject CoinPrefab;
void Start()
{
this.scoreUi = GameObject.Find("Score").GetComponent<Text>();
}
void Update()
{
this.scoreUi.text = string.Format("Score {0}", this.score);
if (this.isDie)
{
GameObject coinGo = Instantiate(CoinPrefab) as GameObject;
coinGo.transform.position = this.enemyPos;
this.isDie = false;
}
}
}
같은 방법으로 폭탄과 파워 아이템 구현 예정.
랜덤한 숫자를 생성하여 아이템 생성 확률 구현 예정
'Unity > 게임 엔진 응용 프로그래밍' 카테고리의 다른 글
| Vertical 2D Shooting 10 : 적기 총알발사 (0) | 2021.10.15 |
|---|---|
| Vertical 2D Shooting 09 : 코루틴으로 무적시간 구현 (0) | 2021.10.15 |
| 코루틴(Coroutine) (0) | 2021.10.14 |
| Vertical 2D Shooting 07 : UI 일부 구현 (점수, 생명) (0) | 2021.10.14 |
| Vertical 2D Shooting 06 : 적기 자동생성, 피격 애니메이션 (0) | 2021.10.14 |