Frage

So I have this piece of code that increments the score once the ball hits the brick but its not incrementing according to the way I think I programmed it to do. When it hits brick3 I want it to increase the Score by 10. I have a variable called bonusBall that is supposed to multiply whatever the score is by 2. bonusBall is only used when the ball hits a brick right after hiting another brick. So when it hits brick2 after it hits brick3 it is suppposed to multiply the score by 2. In this scenerio, the score should equal 40 because it hit brick3 (10 points) then it hit brick2 (10 points) and because it hit brick2 right after hitting brick3 the score gets multiplied by 2 which brings us to the total of 40. My real problem is that when the ball collides with the paddle I need it to bring the bonusBall value back down to 0. I have an If statement for this but it does not seem to work. After the ball hits the paddle the score still multiplies by 2 when it hits a brick. Here is the code :

#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;
    }
War es hilfreich?

Lösung

Just by looking at your code and description it is hard to know exactly what behavior you are looking for, but first let's look at what your code is doing. Look at the comment I've added in the code below:

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");
}

I think what you really want to do is cut comboBall = 2; from the Brick2 collision case and paste to the Brick3 collision case. So, OnCollisionEnter function becomes:

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;
    }
}

This way, the following behavior is implemented:

  • Brick2 is hit - comboBall set to 2.
  • IF next item hit is Brick3:
    • comboBall is 2, so score will be multiplied by 2.
  • ELSE IF next item hit is paddle:
    • comboBall is set to 0.
    • IF next item hit is Brick3:
      • comboBall is 0, so score will not be multiplied.
    • ELSE IF next item hit is Brick2:
      • go back to top bullet point.

Make sense? From your description, this looks like the behavior you're looking for. Let me know if there are any questions.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top