*적기 피격 연출
Enemy Small, Middle, Big 오브젝트에 각각 Enemy A, B, C Hit 이미지를 자식으로 넣는다
EnemyHitTrigger 스크립트를 생성
각각 부모Enemy에 끌어다 넣는다
EnemyHitTrigger 스크립트 열기
코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHitTrigger : MonoBehaviour
{
public int hp = 100;
public GameObject hitGo;
private float delta;
private float span = 0.2f;
private bool isHit = false;
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("---------------->" + collision.name);
this.isHit = true;
this.hitGo.SetActive(true);
}
private void Update()
{
if(this.isHit)
{
this.delta += Time.deltaTime;
if(this.delta >= this.span)
{
this.delta = 0;
this.hitGo.SetActive(false);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletTrigger : MonoBehaviour
{
private GameDirector gameDirector;
private void Start()
{
this.gameDirector = GameObject.Find("GameDirector").GetComponent<GameDirector>();
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Enemy"))
{
var trigger = collision.GetComponent<EnemyHitTrigger>();
trigger.hp -= 1;
if (trigger.hp <= 0)
{
Destroy(this.gameObject);
Destroy(collision.gameObject);
this.gameDirector.AddScore(10);
}
}
}
}
각각의 Enemy 부모 오브젝트를 누르고 EnemtHitTrigger 스크립트에서 Hp 100 확인
HitGo에 각각 Enemy A Hit 자식 오브젝트를 끌어다 넣는다
*피격 시 깜빡임 완성
'Unity > 수업 내용' 카테고리의 다른 글
[2D]프로젝트Play&plane - 적 기체 제거 (0) | 2021.10.14 |
---|---|
[2D]프로젝트Play&plane - 적기 피격 시 미사일 불통과 (0) | 2021.10.14 |
[2D]프로젝트Play&plane - 생명력 표시(3개) (0) | 2021.10.14 |
[2D]프로젝트Play&plane - 점수판 만들기 (0) | 2021.10.14 |
[2D]프로젝트Play&plane - 스케줄링 (0) | 2021.10.14 |