箱子留下剩余物品
本课属于 Coddy Unity 学习旅程的“物理与游戏玩法”单元。第 18 课,共 71 课。
挑战
简单让箱子保留放不下的弹药。
- 在
AmmoCrate.cs中,将AddAmmo那一行替换为amount -= blaster.AddAmmo(amount);。 - 只有当
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 在线编辑器