【发布时间】:2019-12-04 08:49:23
【问题描述】:
我正在制作一个无尽的跑步游戏。并希望在达到一定分数时增加健康/生命。在附加到 ScoreManager 的 ScoreManager 脚本中,我有:
public class ScoreManager : MonoBehaviour
{
public int score;
public Text scoreDisplay;
bool one = true;
Player scriptInstance = null;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Obstacle"))
{
score++;
Debug.Log(score);
}
}
// Start is called before the first frame update
void Start()
{
GameObject tempObj = GameObject.Find("ghost01");
scriptInstance = tempObj.GetComponent<Player>();
}
// Update is called once per frame
private void Update()
{
scoreDisplay.text = score.ToString();
if (scriptInstance.health <= 0)
{
Destroy(this);
}
if (score == 75 || score == 76 && one == true)
{
scriptInstance.health++;
one = false;
}
}
我使用以下几行来增加里程碑的运行状况,但必须无休止地复制代码以创建多个里程碑。
if (score == 75 || score == 76 && one == true)
{
scriptInstance.health++;
one = false;
}
我的问题是如何在不重复代码的情况下每75点增加生命值?
【问题讨论】:
-
你可以使用模数。检查
if (score % 75 == 0) { //addHealth} -
@Willie 也是我的第一个想法,但在分数保持在
75时仍然始终返回 true ... -
如果里程碑定期间隔 Willie's 将是我的建议,否则,我有一系列里程碑,并且在添加分数时,如果它等于里程碑等
-
@Willie 如果添加一个额外的标志,你也可以简单地添加第二个专用计数器;)
-
@derHugo 是的。两种解决方案都一样好。