Adding extra functions to the rocket

My code, whilst working for some parts wont work if I have it the exact same way for others.
In the example below to get the particle effect in my landing sequence to play I cannot have them inside an if statement, yet when going through with Rick I needed to put the main booster particles in an if statement.

 private void StartLanding()
    {
        rb.AddRelativeForce(Vector3.up * landingThrust * Time.deltaTime);
        if(!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(landingAfterBurner);
        }
        landingBoosterParticles.Play();
        Debug.Log("afterburners are burning");
    }
     private void StopLanding()
    {
        landingBoosterParticles.Stop();
        Debug.Log("afterburners stopped burning");
    }


   

    

    private void StartThrust()
    {
        rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
        if (!audioSource.isPlaying)
        {
            audioSource.PlayOneShot(mainEngineThruster);
        }
        if (!mainBoosterParticles.isPlaying)
        {
            mainBoosterParticles.Play();
        }
    }
    private void StopThrust()
    {
        audioSource.Stop();
        mainBoosterParticles.Stop();
    }

I’m not understanding as when we created the left and right booster particle effects, they also needed to to inside an if, else statement the same as the mainThrusterParticles.

private void RotateLeft()
    {
        ApplyRotation(rotationThrust);
        if (!leftBoosterParticles.isPlaying)
        {
            leftBoosterParticles.Play();
        }
    }

    private void RotateRight()
    {
        ApplyRotation(-rotationThrust);
        if (!rightBoosterParticles.isPlaying)
        {
            rightBoosterParticles.Play();
        }
    }

    private void StopRotation()
    {
        leftBoosterParticles.Stop();
        rightBoosterParticles.Stop();
    }

also my audio for my after burners doubles up and I get that disgusting clicking sound. Rick’s fix for this
was to create the else statement audioSource.Stop(); method call. Yet when I do this It causes my mainEngineThruster audio clip to not play anymore and I still get the double up of sound on the afterburners sound clip. This has caused me to not go ahead and add the sound to the left and right turn thrusters, even though Rick claimed that we should already know how to do it as we went over it earlier in the course.

Hi Kontroll,

It’s great to see that you are challenging yourself. :slight_smile:

Regarding your ideas, don’t hestitate to modify Rick’s code.

Ideally, we want to check whether the particle system is already playing before calling its Play(). Otherwise, it might be that the particle system gets reset. We do the same for the audio source.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? If you get click sounds, it might be that the audio source gets stopped and started every other frame. This is not what is supposed to happen in the game. We want to stop the audio source only when we do not move the rocket anymore.

A common mistake is to stop the audio source every other frame. Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

If necessary, draw the logic flow with a pen on a sheet of paper. That’ll make it easier to write the if-statements. The concepts in this game are not difficult but we have quite a few ideas, so the code is fairly long. We have to pay close attention to the curly brackets, so we do not inadvertendly declare an if-statement within another if-statement (unless that’s required by our concept).


See also:

Privacy & Terms