유니티 심화/복습

[복습] SpaceShooter2D - 적 비행기 피격시 애니메이션, 총알 삭제

다모아 2023. 8. 29. 15:57

 

MonsterController

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

public class MonsterController : MonoBehaviour
{
    //애니메이션 가져오기
    private Animator anim;
    //몬스터체력
    private int hp = 3;
    public int Hp
    {
        get
        {
            return this.hp;
        }
    }
    //최대체력
    private int maxHp;
    // Start is called before the first frame update
    void Start()
    {
        this.anim = this.GetComponent<Animator>();
        //최대체력넣기
        this.maxHp = hp;
    }

    private void Update()
    {

    }

    //맞았을 때
    public void Hit()
    {
        this.StartCoroutine(this.CoHit());
    }

    private IEnumerator CoHit()
    {
        this.PlayAnimation(EnumState.eMonsterState.Hit);
        yield return new WaitForSeconds(0.20f);
        this.PlayAnimation(EnumState.eMonsterState.Idle);
    }

    //애니메이션 메서드함수
    private void PlayAnimation(EnumState.eMonsterState state)
    {
        this.anim.SetInteger("State", (int)state);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.CompareTag("Bullet"))
        {
            //총알이 몬스터한테 맞음
            this.Hit();
        }
    }
}

 

EnumState

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

public class EnumState
{
    public enum ePlayerState
    {
        Center, Left, Right
    }

    public enum eMonsterState
    {
        Idle, Hit
    }
}