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
}
}
'유니티 심화 > 복습' 카테고리의 다른 글
[복습] HeroShooter - 총알 CollisionEnter or TriggerEnter (0) | 2023.08.29 |
---|---|
[복습] HeroShooter - 스테이지1 (0) | 2023.08.29 |
[복습] SpaceShooter2D - CollisionEnter2D[오류] (0) | 2023.08.29 |
[복습] SpaceShooter2D - 오브젝트 풀링, 총알 나가기 (0) | 2023.08.29 |
[복습] SpaceShooter2D - Player, Bullet -> Main으로 옮기기, 오브젝트 풀링 기반 만들기 (0) | 2023.08.28 |