2
\$\begingroup\$

The idea is to make the sound work when comboTime is false.

However, it works in an unexpected way: the sound works after the last if statement within ComboHud.js, which is:

if (Time.time > Combo.timeBlockLastDestroyed + 2f){
    blocksd.enabled = false;
    blocksd.fontSize = 16;
    totalComboHits = 0;
}

This delay depends on the value I set inside the comparison. I still can't find out which move I did wrong.

Current issue: message shows up, then message dissapears, then the sound is played.

Goal: message shows up and the sound is played, then message dissapears.

Screenshot

(ask me if you need more screens)

Scripts involved

  • Combo.js - mechanics of the combo.
  • ComboHud.js - basically the display of the combo text.
  • Announcer.js - loads the audio files.

Combo.js

#pragma strict

public class Combo extends MonoBehaviour{
    public static var timeBlockLastDestroyed : float = 0;
    public static var blocksDestroyed : int = 0;
    public static var blocksDestroyedRecord : int = 0;
    public static var comboTime : boolean = false;
    public static var bonusScore : int = 0;
    public static var bonusFactor : float = 1;

    function Awake(){
    }

    function Update () {
        if(Time.time >= timeBlockLastDestroyed + 1f && blocksDestroyed > 0){
            Score.score = Score.score + bonusScore;
            blocksDestroyed = 0;
            bonusScore = 0;
            comboTime = false;
        }
    }
    
    function BlockWasDestroyed(){
        timeBlockLastDestroyed = Time.time;
        blocksDestroyed++;
        CheckBonusFactor(blocksDestroyed);
        comboTime = true;
        if(blocksDestroyed > 2){ bonusScore = (blocksDestroyed * 20)/2 + bonusScore * bonusFactor;}
        
    }
    
    function CheckBonusFactor(blocksd : int){
        if(blocksd > 2){
            bonusScore = 10;
            bonusFactor = 1.5;
        }
        if(blocksd > 5){
            bonusFactor = 2.5;
        }
        if(blocksd > 8){
            bonusFactor = 4;
        }
        if(blocksd >= 12){
            bonusFactor = 6;
        }
        if(blocksd >= 15){
            bonusFactor = 10;
        }
        if(blocksd > 20){
            bonusFactor+=2;
        }
        
    }
}

ComboHud.js

#pragma strict

public class ComboHud extends MonoBehaviour{

    public var announcerObj : GameObject;
    
    private var blocksd : GUIText;
    private var totalComboHits : int;
    private var announcer : Announcer;
    private var announcerVoice : AudioSource;
    private var comboTitles : String[];
    
    function Awake(){
        blocksd = GetComponent(GUIText);
        announcer = announcerObj.GetComponent(Announcer);
        announcerVoice = announcerObj.GetComponent(AudioSource);
        comboTitles = ["","","","Triple Combo","Quad Combo", "Solid Combo", "Hyper Combo", "Brutal Combo", "Master Combo", "Blaster Combo", "Extreme Combo!", "Awesome Combo!", "Monster Combo!", "INSANE COMBO !!", "BEASTLY COMBO !!", "KING COMBO !!", "CRAZY COMBO !!!", "KILLER COMBO", "GODLIKE COMBO"];
        totalComboHits = Combo.blocksDestroyed;
    }
    
    function Update () {
        blocksd.text = Combo.blocksDestroyed + ' hits';
        if (Combo.blocksDestroyed > 2 && Time.time <= Combo.timeBlockLastDestroyed + 1f){
            blocksd.enabled = true;
            blocksd.fontSize = 16 + Combo.blocksDestroyed;
            totalComboHits = Combo.blocksDestroyed;
        }
        
        if (!Combo.comboTime){
            if (totalComboHits >= 3 && totalComboHits < 18){
                announcerVoice.clip = announcer.voiceSounds[totalComboHits];
                announcerVoice.Play();
                blocksd.text = totalComboHits + ' hits\n'+ comboTitles[totalComboHits];
            }
            if (totalComboHits >= 18){
                announcerVoice.clip = announcer.voiceSounds[18];
                announcerVoice.Play();
                blocksd.text = totalComboHits + ' hits\n'+ comboTitles[18];
            }
        }
        
        if (Time.time > Combo.timeBlockLastDestroyed + 2f){
            blocksd.enabled = false;
            blocksd.fontSize = 16;
            totalComboHits = 0;
        }
    }
}

Announcer.js

#pragma strict
#pragma downcast

public class Announcer extends MonoBehaviour{

    public var voice : AudioSource;
    
    public var voiceSounds = Array();
    function Awake(){
        voiceSounds[0] = Resources.Load("Sounds/null");
        voiceSounds[1] = Resources.Load("Sounds/null");
        voiceSounds[2] = Resources.Load("Sounds/null");
        voiceSounds[3] = Resources.Load("Sounds/Announcer/triple");
        voiceSounds[4] = Resources.Load("Sounds/Announcer/quad");
        voiceSounds[5] = Resources.Load("Sounds/Announcer/solid");
        voiceSounds[6] = Resources.Load("Sounds/Announcer/hyper");
        voiceSounds[7] = Resources.Load("Sounds/Announcer/brutal");
        voiceSounds[8] = Resources.Load("Sounds/Announcer/master");
        voiceSounds[9] = Resources.Load("Sounds/Announcer/blaster");
        voiceSounds[10] = Resources.Load("Sounds/Announcer/extreme");
        voiceSounds[11] = Resources.Load("Sounds/Announcer/awesome");
        voiceSounds[12] = Resources.Load("Sounds/Announcer/monster");
        voiceSounds[13] = Resources.Load("Sounds/Announcer/insane");
        voiceSounds[14] = Resources.Load("Sounds/Announcer/beastly");
        voiceSounds[15] = Resources.Load("Sounds/Announcer/king");
        voiceSounds[16] = Resources.Load("Sounds/Announcer/crazy");
        voiceSounds[17] = Resources.Load("Sounds/Announcer/killer");
        voiceSounds[18] = Resources.Load("Sounds/Announcer/godlike");
    }
    
    function Update () {
        
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Whoa wait, why off-topic? \$\endgroup\$
    – Fabián
    May 14 '14 at 1:47

Browse other questions tagged or ask your own question.