Play One Shot no sound at all

No way I can make my PlayOneShot work with the sounds. The only way I can make the sound work is if I use the audio on AudioSource ( Which I can use only one sound). I tried to check the volume, is not the case. I used Debug.Log to check if Stop sound is overcoming PlayOneSound (is not the case). I desactivated the collision Script to see if there is anything cause trouble (not the case). Tried different audios, checked if my Unity is without sound (no the case because if I use mainEngine on AudioSource component it works just fine. Any more Ideas on what I can try?? I will Skip this lesson for now since I have been breaking my head with this for days!

This is my code:

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

public class Movement : MonoBehaviour
{
Rigidbody Rb;
AudioSource audioSource;
public AudioClip mainEngine;

public float thrustSpeed = 1000.0f;
private float rotationThrust = 100.0f;

void Start()
{
    Rb = GetComponent<Rigidbody>();
    audioSource = GetComponent<AudioSource>();
}
void Update()
{
    ProcessThrust();
    ProcessRotation();
}

void ProcessThrust()
{
    if (Input.GetKey(KeyCode.UpArrow))
    {
        Rb.AddRelativeForce(Vector3.up * Time.deltaTime * thrustSpeed);
        if (!audioSource.isPlaying)
        {
            Debug.Log("Audio Playing");
            audioSource.PlayOneShot(mainEngine, 1.0f);
        }
    }
    else
    {
        Debug.Log("Audio Not playing");
        audioSource.PlayOneShot(mainEngine, 1.0f);
    }
}

void ProcessRotation()
{
    if (Input.GetKey(KeyCode.LeftArrow))
    {
        applyRotation(rotationThrust);
    }
    if (Input.GetKey(KeyCode.RightArrow))
    {
        applyRotation(-rotationThrust);
    }
}
private void applyRotation(float rotateThisFrame)
{
    Rb.freezeRotation = true; // stops rigidbody rotation to act while player is turning rotation, this fix bug about colission when rocket collides the control gets crazy 
    transform.Rotate(Vector3.forward * Time.deltaTime * rotateThisFrame);
    Rb.freezeRotation = false; // after player is not using command to rotate anymore, it returns to the normal
}

}

This is the SS of unity:

You’ve already asked this question

Here’s some tips

Double check your pitch because that could be what’s wrong(I believe it’s wrong, should be 1)

You want to actually play the sound before testing it.

Inside of your if statement(the one that controls the output after hitting the up arrow), put your audio there, instead of putting it inside of another if statement to see if audio has been done.

Also when your checking to see if audio is playing, your basically saying:

If audio IS NOT playing, then debug.log that it is playing

I’m also pretty sure when I did this course I did this:

  1. Put the audio Source that you want to use in the Audio Source component (very first option)

  2. Create a SerializedField for the audio Source (the component that is storing your sound)

3.attach the component to your SerializedField variable

  1. Inside of your if statement put your playoneshot stuff

  2. Inside of Unity disable playonawake

That’s what I did, but ultimately I would double check your code to find anything

Hope this helps!

1 Like

Great suggestions on how to debug one’s project! Thanks for sharing them. :slight_smile:

Thanks for the feedback, Nina!

Yess! Thank you very much man! very helpfull insight, sorry for late response!! I turned out the Pitch was the cause and totally solved my problem!

Your welcome! :slight_smile:

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

Privacy & Terms