简体   繁体   English

达到分数时如何增加健康

[英]How to increase health when score has reached

I'm making a endless runner game.我正在制作一个无尽的亚军游戏。 And would like to increase the health/life when a certain score has reached.并希望在达到某个分数时增加健康/生命。 In a ScoreManager script attached to a ScoreManager I have:在附加到 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;
    }
}

I used the following lines to increase the health at a milestone, but have to copy the code endlessly to create multiple milestones.我使用以下几行来增加里程碑的健康状况,但必须无休止地复制代码以创建多个里程碑。

if (score == 75 || score == 76 && one == true)
{
    scriptInstance.health++;
    one = false;
}

My question is how to increase health every 75 points, without duplicating the code?我的问题是如何在不重复代码的情况下每增加 75 点生命值?

The issue with a modulo like if(score % 75 == 0) would be that it returns true all the time while score == 75 .. so it would require an additional bool flag anyway.if(score % 75 == 0)这样的模的问题是它一直返回truescore == 75 .. 所以无论如何它都需要一个额外的bool标志。

I would rather simply add a second counter for this!我宁愿简单地为此添加第二个计数器!

And not check things repeatedly in Update at all but rather the moment you set it:并且根本不在Update中重复检查内容,而是在您设置它的那一刻:

int healthCounter;

void OnTriggerEnter2D(Collider2D other)
{
    if (other.CompareTag("Obstacle"))
    {
        score++;
        Debug.Log(score);

        // enough to update this when actually changed
        scoreDisplay.text = score.ToString();

        healthCounter++;
        if(healthCounter >= 75)
        {
            scriptInstance.health++;

            // reset this counter
            healthCounter = 0;
        }
    }
}

the one drawback maybe is that know you have to reset the healthCounter = 0 wherever you reset the score = 0 ... but you would have to do the same with any flag solution as well ;)一个缺点可能是知道您必须在重置healthCounter = 0任何地方重置healthCounter = 0 score = 0 ...但您也必须对任何标志解决方案执行相同的操作;)

I would go with th % operator我会和%运算符一起去

private bool scoreChanged = false;
void OnTriggerEnter2D(Collider2D other)
{
    if (other.CompareTag("Obstacle"))
    {
        score++;
        scoreChanged = true;
        Debug.Log(score);
    }
}

if (score % 75 == 0 && scoreChanged)
{
    scriptInstance.health++;
    scoreChanged = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM