public class Bullet : MonoBehaviour { public int power = 10; void Start (){} void Update (){} }
public class EnemyHit : MonoBehaviour { public int hp = 1000; void Start(){} void Update(){} void OnTriggerEnter (Collider col) { if (col.gameObject.tag == "Bullet") { int bulletPower = col.gameObject.GetComponent<Bullet> ().power; Destroy (col.gameObject); hp -= bulletPower; if (hp <= 0) Destroy (gameObject); } } }
자동 발사되는 총알이 날아가서 적에게 부딫치면 적의 hp를 깎고, 0이되면 적을 없엔다.