I understand the Methods BUT

Methods makes the code more readable but what if you need to go in and change something in the method. For instance, I would like to add another sound so the thrusters make a different sound than the main engine. I have the audio file I made just for that but now I am not sure where and how to implement a second audio source.
It is problem a simple thing but I am new to coding or at least have been away from any kind of coding since the days Basic was the language everyone used. (I guess that dates me!)

I assume it would start something like this:

[SerializeField] AudicClip thrusters;

// then somewhere farther down in the code where the thrusters are engaged in //ProcessRotation:

StartPlaying(thrusters);

// but I don’t know where for sure because it is all hidden in the StartThrusting(); method we made.

Hi Richard,

I would say: Write your code and test it. Then you will see (or hear) if it works. In most cases, the compiler will tell you if you made a mistake. And if something does not work as expected and you cannot spot the issue, you could use a Debug.Log to analyse the problem.

We create methods to organise our code. Whether you put everything into one method or treat your thruster audio as a new task, is up to you. The computer does not care but future-you will be glad if your code is readable for humans.

A little hint: The Update method gets called each frame. If you want to process user input, you do it there. In the Update method, you could call other methods.

Good luck! :slight_smile:


See also:

Thanks, I will give your suggestion a try. I appreciate the hints! :slight_smile:

Well, I did solve the problem of adding thruster sounds. It is not very eloquent but it gets the job done. I don’t like the sound I made and I hope I can just swap out the sound file for a different named exactly the same.

Here is the solution that I came up with:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class Movement : MonoBehaviour

{
[SerializeField] float mainThrust = 100f;
[SerializeField] float rotationThrust = 1f;
[SerializeField] AudioClip mainEngine;
[SerializeField] AudioClip thrusters;
[SerializeField] ParticleSystem mainEngineParticles;
[SerializeField] ParticleSystem leftThrusterParticles;
[SerializeField] ParticleSystem rightThrusterParticles;

Rigidbody rb;
AudioSource audioSource;   

// Start is called before the first frame update

void Start()
{
    rb = GetComponent<Rigidbody>();
    audioSource = GetComponent<AudioSource>();
}
// Update is called once per frame

void Update()
{
    ProcessThrust();
    ProcessRotation();
}
void ProcessThrust()
{
    if (Input.GetKey(KeyCode.Space))
    {
        StartThrusting();
    }
    else
    {
        StopThrusting();
    }
}
void ProcessRotation()
{
    if (Input.GetKey(KeyCode.A))
    {        
        RotateLeft();
        audioSource.PlayOneShot(thrusters);
    }
    else if (Input.GetKey(KeyCode.D))
    {
        RotateRight();
        audioSource.PlayOneShot(thrusters);
    }
    else
    {
        StopRotating();
    }
}
    void StartThrusting()
{
    rb.AddRelativeForce(Vector3.up * mainThrust * Time.deltaTime);
    if (!audioSource.isPlaying)
    {
        audioSource.PlayOneShot(mainEngine);
    }
    if (!mainEngineParticles.isPlaying)
    {
        mainEngineParticles.Play();
    }
}
void StopThrusting()
{
    audioSource.Stop();
    mainEngineParticles.Stop();
}
private void RotateLeft()
{
    ApplyRotation(rotationThrust);
    if (!rightThrusterParticles.isPlaying)
    {
        rightThrusterParticles.Play();
    }
}
private void RotateRight()
{
    ApplyRotation(-rotationThrust);
    if (!leftThrusterParticles.isPlaying)
    {
        leftThrusterParticles.Play();
    }
}
private void StopRotating()
{
    rightThrusterParticles.Stop();
    leftThrusterParticles.Stop();
}
void ApplyRotation(float rotationThisFrame)
{
    rb.freezeRotation = true;  // freezing rotation so we can manually control rotation
    transform.Rotate(Vector3.forward * rotationThisFrame * Time.deltaTime);
    rb.freezeRotation = false;  // unfreezing rotation so physics system can take over
}

}

Good job! I knew you would be able to solve this problem yourself! :slight_smile:

1 Like

Well, not all seems to be correct in my sounds for thrusters that I added. I started the Unity program up to do the next assignment and I got a yellow warning at the bottom of the screen which reads:

FMOD failed to set the software format to the custom sample rate 1, trying software fallback rate 48000

I know that has something to do with the sounds and now as I listen to the sounds, no matter what sound I use for the thrusters i.e. the Rocket Blast sound, the Success sound, or the thruster sound, it all sounds the same and it is a very nasty sound (almost machine gun in effect).

What does FMOD failed mean? I looked it up and there wasn’t much info on it (or at least that I could find).

Unfortunately, I wasn’t able to find much either, but this seems to have fixed the issue for some people:
https://forum.unity.com/threads/fmod-failed-to-initialize.602845/#post-4035862

If the issue is not caused by the “FMOD failed” error, add Debug.Logs to see where the issue might stem from. Call Play methods only if the audio source is not playing. Otherwise, it might be that the audio source constantly stops and restarts, which would explain the undesired sound effects.

If you fixed the “machine gun” sound and the volume increases, please follow this tutorial:

The sound problem itself only happens to the additional audio clip that I added. The original sound effect that I made for the rocket blast is fine. However, even if I remove all the added code for the addition sound for the thruster, I still get that FMOD error when I first start the Unity Project. The warning goes away after I play the game once. But, every time I start Unity, the ERROR about FMOD is shown. The article you showed me does not seem to be my problem as there are no NAHIMIC files on my computer (that I can find). I have no blue tooth devices connected to my computer which is what I gather NAHIMIC is used for.

On further investigations, I noticed in the error message that part of the failure was that the sample = 1. I went into project settings and looked at the Audio settings and the sample rate was set to 1. I changed that to 44100 (which is what the audio I recorded was sampled at. When I restarted the Unity Project that FMOD error had disappeared.

I guess this is solved even though I don’t know how the sample rate got changed in the first place.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms