【问题标题】:Unity 3D How do I get my score to increment properly?Unity 3D 如何让我的分数正确增加?
【发布时间】:2014-03-07 17:18:55
【问题描述】:

所以我有这段代码,一旦球击中砖块,分数就会增加,但它不会按照我认为我编程它的方式增加。当它击中brick3 时,我希望它增加10 的分数。我有一个名为bonusBall 的变量,它应该将分数乘以2。仅当球在击中另一块砖后立即击中砖时才使用bonusBall。因此,当它在击中brick3 后击中brick2 时,应该将分数乘以2。在这个场景中,分数应该等于40,因为它击中了brick3(10 分),然后它击中了brick2(10 分)并且因为它击中了brick2击中brick3后,分数乘以2,总数为40。我真正的问题是,当球与桨发生碰撞时,我需要将bonusBall值降为0。我有一个If语句但它似乎不起作用。球击中球拍后,当球击中砖块时,分数仍乘以 2。这是代码:

#pragma strict

private var score : int=0;
private var comboBall : int=0;
var guiScore : GUIText;

var blue : Texture;

var isBlue : boolean = false; 

var particle3 :boolean = false;

var brickPieces : ParticleSystem;

var brickPieces2 : ParticleSystem;

function Start () {
    guiScore.text="Score: 0";
}

function OnCollisionEnter(col : Collision){

    if(col.collider.name == "Brick3"){
        Instantiate(brickPieces,transform.position,transform.rotation);
        brickPieces.Play();
        particle3 = true;
        Destroy(col.gameObject);
        score += 10;
        guiScore.text= "Score: " + score;
        renderer.material.mainTexture = blue;
        isBlue = true; 
    }

    if(col.collider.name == "Brick2" && isBlue == true){
        Instantiate(brickPieces2,transform.position,transform.rotation);
        brickPieces2.Play();
        Destroy(col.gameObject);
        score += 10;
        comboBall = 2;
        if(comboBall == 2){ 
            score = comboBall*score;
        }

        guiScore.text= "Score: " + score;
        print("collided with brick");
    }

    if(col.collider.name == "paddle"){
        comboBall = 0;
    }

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    仅通过查看您的代码和描述很难确切知道您在寻找什么行为,但首先让我们看看您的代码在做什么。看看我在下面代码中添加的注释:

    if(col.collider.name == "Brick2" && isBlue == true){
        Instantiate(brickPieces2,transform.position,transform.rotation);
        brickPieces2.Play();
        Destroy(col.gameObject);
        score += 10;
    
        // HERE...comboBall will always be set to 2, so
        // the following if statement is useless.  Your
        // score will always be multiplied by two.
        comboBall = 2;
        if(comboBall == 2){ 
            score = comboBall*score;
        }
    
        guiScore.text= "Score: " + score;
        print("collided with brick");
    }
    

    我认为您真正想要做的是将comboBall = 2; 从 Brick2 碰撞案例中剪切并粘贴到 Brick3 碰撞案例中。所以,OnCollisionEnter函数变成:

    function OnCollisionEnter(col : Collision){
    
        if(col.collider.name == "Brick3"){
            Instantiate(brickPieces,transform.position,transform.rotation);
            brickPieces.Play();
            particle3 = true;
            Destroy(col.gameObject);
            score += 10;
            guiScore.text= "Score: " + score;
            renderer.material.mainTexture = blue;
            isBlue = true;
    
            // MOVED COMBO CODE TO HERE
            comboBall = 2; 
        }
    
        if(col.collider.name == "Brick2" && isBlue == true){
            Instantiate(brickPieces2,transform.position,transform.rotation);
            brickPieces2.Play();
            Destroy(col.gameObject);
            score += 10;
    
            if(comboBall == 2){ 
                score = comboBall*score;
            }
    
            guiScore.text= "Score: " + score;
            print("collided with brick");
        }
    
        if(col.collider.name == "paddle"){
            comboBall = 0;
        }
    }
    

    这样,实现了以下行为:

    • Brick2 被击中 - comboBall 设置为 2。
    • IF 下一个命中的项目是 Brick3:
      • comboBall 为 2,因此分数将乘以 2。
    • ELSE IF 下一项命​​中是桨:
      • comboBall 设置为 0。
      • IF 下一个命中的项目是 Brick3:
        • comboBall 为 0,所以分数不会相乘。
      • ELSE IF 下一个命中的项目是 Brick2:
        • 返回顶部要点。

    有意义吗?根据您的描述,这看起来像您正在寻找的行为。如果有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 2018-09-25
      • 2021-05-09
      • 2018-07-14
      相关资源
      最近更新 更多