Unity/게임 엔진 응용 프로그래밍

Vertical 2D Shooting 08 : 코인 이이템 드롭

치명적흑형 2021. 10. 15. 11:15

코인 이이템 드롭

 

적기가 죽으면 감독 오브젝트에게 죽었는지 여부와 죽은 위치를 알린다.

감독 오브젝트가 죽었는지 여부와 위치를 판단하여 아이템을 생성한다.

생성된 코인 오브젝트가 플레이어와 충돌하면 점수가 오른다.


 

코인 스크립트

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;
        }
    }
}


같은 방법으로 폭탄과 파워 아이템 구현 예정.

랜덤한 숫자를 생성하여 아이템 생성 확률 구현 예정