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

Vertical 2D Shooting 06 : 적기 자동생성, 피격 애니메이션

치명적흑형 2021. 10. 14. 11:59

적생성

 

반오브젝트 EnemyGenerator + 스크립트 EnemyGenerator

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyGenerator : MonoBehaviour
{
    public GameObject EnemyAPrefab;
    float enemyATime;
    float enemyASpan = 1f;

    public GameObject EnemyBPrefab;
    float enemyBTime;
    float enemyBSpan = 3f;

    public GameObject EnemyCPrefab;
    float enemyCTime;
    float enemyCSpan = 5f;

    public GameObject BossPrefab;
    float bossTime;
    float bossSpan = 10f;

    void Start()
    {

    }

    void Update()
    {
        this.enemyATime += Time.deltaTime;
        this.enemyBTime += Time.deltaTime;
        this.enemyCTime += Time.deltaTime;
        this.bossTime += Time.deltaTime;

        if (this.enemyATime > this.enemyASpan)
        {
            this.enemyATime = 0;
            GameObject enemyAGo = Instantiate(EnemyAPrefab) as GameObject;
            int px = Random.Range(-5, 5);
            enemyAGo.transform.position = new Vector3(px, 5.5f, 0);
        }
        if (this.enemyBTime > this.enemyBSpan)
        {
            this.enemyBTime = 0;
            GameObject enemyBGo = Instantiate(EnemyBPrefab) as GameObject;
            int px = Random.Range(-5, 5);
            enemyBGo.transform.position = new Vector3(px, 5.5f, 0);
        }
        if (this.enemyCTime > this.enemyCSpan)
        {
            this.enemyCTime = 0;
            GameObject enemyCGo = Instantiate(EnemyCPrefab) as GameObject;
            int px = Random.Range(-5, 5);
            enemyCGo.transform.position = new Vector3(px, 5.5f, 0);
        }
        if (this.bossTime > this.bossSpan)
        {
            this.bossTime = 0;
            GameObject bossGo = Instantiate(BossPrefab) as GameObject;
            int px = Random.Range(-5, 5);
            bossGo.transform.position = new Vector3(px, 5.5f, 0);
        }
    }
}

필드에 프리팹 붙여주기


적 HP 감소 파괴

 

Enemy 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyA : MonoBehaviour
{
    public float hp;
    public GameObject playerBullet;
    public GameObject playerMainBullet;
    public GameObject playerSubBullet;
    void Start()
    {
        this.playerSubBullet = this.playerBullet.transform.GetChild(0).gameObject;
        this.playerMainBullet = this.playerBullet.transform.GetChild(1).gameObject;
    }

    void Update()
    {
        this.transform.Translate(Vector2.down * 2 * Time.deltaTime);

        //처치당하면
        if (this.hp <= 0)
        {
            Destroy(this.gameObject);
        }

        //화면밖으로 나가면
        if (this.gameObject.transform.position.y <= -6.0f)
        {
            Destroy(this.gameObject);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Bullet"))
        {
            //this.hp -= 1;     //총알 데미지
            if (collision.gameObject.name == "SubBullet")
            {
                this.hp -= this.playerSubBullet.GetComponent<BulletTrigger>().damage;
            }
            if (collision.gameObject.name == "MainBullet")
            {
                this.hp -= this.playerMainBullet.GetComponent<BulletTrigger>().damage;
            }
        }
    }
}

 

Bullet 스크립트

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletTrigger : MonoBehaviour
{
    public float damage;
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Enemy"))
        {
            Destroy(this.gameObject);
        }
    }
}

피격 애니메이션 넣기

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyA : MonoBehaviour
{
    public float hp;
    public GameObject playerBullet;
    public GameObject playerMainBullet;
    public GameObject playerSubBullet;
    Animator animator;
    bool isHit = false;

    void Start()
    {
        this.playerSubBullet = this.playerBullet.transform.GetChild(0).gameObject;
        this.playerMainBullet = this.playerBullet.transform.GetChild(1).gameObject;
        this.animator = GetComponent<Animator>();
    }

    void Update()
    {
        this.transform.Translate(Vector2.down * 2 * Time.deltaTime);

        this.animator.SetBool("isHit", isHit);
        this.isHit = false;
        //처치당하면
        if (this.hp <= 0)
        {
            Destroy(this.gameObject);
        }

        //화면밖으로 나가면
        if (this.gameObject.transform.position.y <= -6.0f)
        {
            Destroy(this.gameObject);
        }

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Bullet"))
        {
            if (collision.gameObject.name == "SubBullet")
            {
                this.hp -= this.playerSubBullet.GetComponent<BulletTrigger>().damage;
                this.isHit = true;
            }
            if (collision.gameObject.name == "MainBullet")
            {
                this.hp -= this.playerMainBullet.GetComponent<BulletTrigger>().damage;
                this.isHit = true;
            }
        }
    }
}