打进球洞
本课属于 Coddy Unity 学习旅程的“物理与游戏玩法”单元。第 69 课,共 71 课。
挑战
困难让一颗慢速弹珠落入球洞。
- 在
Hole中,添加void OnTriggerEnter(Collider other)。 - 当
other.TryGetComponent(out Golf golf)和other.attachedRigidbody.linearVelocity.magnitude < sinkSpeed满足时,记录"In the cup in " + golf.strokes + " strokes!",并调用other.gameObject.SetActive(false);。
按下“开始”,让弹珠入洞:快速弹珠会滚过球洞。然后点击“检查”。
亲手试试
using UnityEngine;
public class Hole : MonoBehaviour
{
[SerializeField] private float sinkSpeed = 3f;
// 弹珠滚得慢就会掉进杯子,滚得快就会滚过去。
}
物理与游戏玩法中的所有课
自己练习: Unity 在线编辑器