Unity 3D如何让我的分数正确增加?

时间:2014-03-07 06:24:21

标签: unity3d

所以我有这段代码,一旦球击中砖块就会增加分数,但是根据我认为我编程的方式不会增加。当它击中brick3时,我希望它将得分增加10.我有一个名为bonusBall的变量,它应该乘以得分为2. bonusBall仅在球击中另一块砖后立即击中砖时使用。所以当它击中brick2之后击中brick3时,它会将得分乘以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;
    }

1 个答案:

答案 0 :(得分: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");
}

我认为你真正想做的是从 Brick2 碰撞案例中剪切comboBall = 2;并粘贴到 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 下一项命中是paddle:
    • comboBall设置为0。
    • IF 下一个项目是Brick3:
      • comboBall为0,因此得分不会成倍增加。
    • ELSE IF 下一个项目是Brick2:
      • 回到顶部要点。

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