[SOLVED] Project Boost | Adding a Touch of Audio

So, having followed the instructions detailing how to add audio to the project, The sound clip doesn’t seem to want to play when keys are pressed.

-----------------------------------------------------------EDIT--------------------------------------------------------
[SOLVED]

Thanks guys so much for your feedback and assistance with the editor. As it turns out however, between the poor quality of my speakers and the low pitch of the clip, it was very hard to hear if it was playing properly.

Also I may have instanciated the sourceAudio.Stop incorrectly.

Hi Bobby,

Does the rocket fly when you press a button?

It does, yes.

[SPACE] adds force to Vector3.up
[A] adds force to Vector3.left
[D] adds force to Vector3.right

All these seem to be functional for the ship but not for the audio.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rocket : MonoBehaviour {

    Rigidbody rigidBody;
    AudioSource audioSource;

    // Start is called before the first frame update
    void Start() {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update() {
        ProcessInput();
    }

    private void ProcessInput()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            rigidBody.AddRelativeForce(Vector3.up);
            if (!audioSource.isPlaying)
            {
                audioSource.Play();
            }
        }

        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward);
            if (!audioSource.isPlaying)
            {
                audioSource.Play();
            }
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward);
            if (!audioSource.isPlaying)
            {
                audioSource.Play();
            }
        }
    }
}

First of all, click on this button:

image

Test your game. Can you hear the sound? Also check in the game window whether there is a “Mute Audio” button. I usually have my audio muted when working in Unity. Maybe your audio is muted, too.

If not, call audioSource.Play() in the Start() method for testing purposes. Do not press any key. Can you hear the sound now?

You may also need to set loop to “true” since you are using a newer version of Unity

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

Privacy & Terms