Menu
CoddyTech

残りはクレートに保管

CoddyのUnityジャーニー「物理演算とゲームプレイ」セクションの一部。レッスン 18/71。

challenge icon

チャレンジ

簡単

箱に、収まりきらなかった弾薬を残しておきましょう。

  1. AmmoCrate.csで、AddAmmoの行をamount -= blaster.AddAmmo(amount);に置き換えます。
  2. amount == 0のときだけDestroy(gameObject);を呼び出します。

「プレイ」を押して右に歩きます。収まりきらなかった2個を保持したまま、Crate2が残ります。その後、「確認」を押します。

自分で試してみよう

using UnityEngine;

public class AmmoCrate : MonoBehaviour
{
    public int amount = 3;

    void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent(out Blaster blaster))
        {
            blaster.AddAmmo(amount);
            Destroy(gameObject);
        }
    }
}

物理演算とゲームプレイのすべてのレッスン

自分で練習してみよう: Unityプレイグラウンド