Menu
CoddyTech

箱子留下剩余物品

本课属于 Coddy Unity 学习旅程的“物理与游戏玩法”单元。第 18 课,共 71 课。

challenge icon

挑战

简单

让箱子保留放不下的弹药。

  1. 在 AmmoCrate.cs 中,将 AddAmmo 那一行替换为 amount -= blaster.AddAmmo(amount);。
  2. 只有当 amount == 0 时才调用 Destroy(gameObject);。

按下“开始”并向右走:Crate2 会留在原地,装着放不下的 2 发弹药。然后点击“检查”。

亲手试试

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 在线编辑器