
- Enemy (적)
플레이어와 총알을 만들었다.
이제 적을 만들어보자.
플레이어 만들듯이 Square로 만든다음 boxcollider2d, rigidbody2d를 부착하고 Enemy 레이어
를 추가해준다음, Enemy 클래스를 만들어서 달아주겠다.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float maxHp;
private float curHp;
public void Damage(float amount)
{
curHp -= amount;
}
private void Awake()
{
curHp = maxHp;
}
private void Update()
{
if(curHp <= 0) Destroy(gameObject);
}
}
그리고 총알 스크립트를 수정하겠다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public float range; //1)
public float speed;
public Vector2 dir;
private void Start()
{
Destroy(gameObject, 3);
}
private void Update()
{
transform.Translate(dir.normalized * speed * Time.deltaTime);
var hit = Physics2D.OverlapCircle(transform.position, range, LayerMask.GetMask("Enemy")); //2)
if(hit != null)
{
hit.GetComponent<Enemy>()?.Damage(10); //3)
Destroy(gameObject);
}
}
}