消息关闭
    暂无新消息!

一旦球(ball)击中砖块(brick),这一段代码就增加得分。但是它并没有按照我所想象的方式递增。当它击中brick3的时候,我想给分数加10分。有个叫做bonusBall的变量,用来把任何分数乘以2。只有在球连续击中砖块时才会使用到bonusBall。所以当撞击了brick3后又撞击brick2,就应该把分数乘以2。在这种情况下得分应该是40,因为撞击brick3(10分),然后撞击brick2(10分),又因为是连击,分数就要乘以2,所以一共得了40分。真正困扰我的问题是:球撞到了让bonusBall值回落到0的paddle。有个IF语句来实现它,但是好像不管用。球撞到paddle后,再撞到砖块,分数仍然乘以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;
    }

原问题:Unity 3D How do I get my score to increment properly?


1个回答

︿ 0

看了你的代码和描述后,我不知道你想要的到底是什么效果。先不管那些,我们先来看看你的代码是做什么的。看看我给你的代码添加的评论。

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;
    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 撞击物体是 paddle:
· comboBall 设置为0.
· IF 下一个撞击物体是 Brick3:
· comboBall 是 0, 所以分数不用乘
· ELSE IF 下一个撞击物体是 Brick2:
· 回到原点.

明白吗?从你的描述中,这似乎是你想实现的效果。如果有问题请告诉我。