Search Unity

PlayOneShot messing with my head

Discussion in 'Scripting' started by olidadda, Dec 9, 2019.

  1. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    37
    Hi all! Really stuck on this... my audio for Engine and Rotate work fine, but for some reason the Die sound just doesn't play all the way through. if I take away the hasPlayed bool it stutters and plays back each frame, but now it just plays for a split second and cuts off.

    Also, if I try to run the audio in the OnCollisionEnter function in the default case, that doesn't work either (although everything else in the function works!)

    I have found a different system for playing audio, but this is a lòearning experience and I would like to know what's going on.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4. public class Rocket : MonoBehaviour
    5. {
    6.  
    7.     //Member variables
    8.     Rigidbody rigidBody;
    9.  
    10.     [SerializeField] AudioClip Engine;
    11.     [SerializeField] AudioClip Rotate;
    12.     [SerializeField] AudioClip Die;
    13.     [SerializeField] AudioClip winLevel;
    14.     AudioSource audio;
    15.    
    16.    
    17.     [SerializeField] float rcsThrust = 220f;
    18.     [SerializeField] float mainThrust = 50f;
    19.     [SerializeField] float upThrust = 50f;
    20.  
    21.     enum State {alive, dying, transcending};
    22.     State state = State.alive;
    23.  
    24.     //[SerializeField] float rotateForce = 20f;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         rigidBody = GetComponent<Rigidbody>();
    30.         audio = GetComponent<AudioSource>();
    31.                
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     { if (state == State.alive)
    37.         {
    38.             ThrustShip();
    39.             RotateShip();
    40.            
    41.         }
    42.        
    43.         Audio();
    44.        
    45.     }
    46.  
    47.     private void ThrustShip()
    48.     {
    49.         if (Input.GetKey(KeyCode.Space) )
    50.         {
    51.             rigidBody.AddRelativeForce(Vector3.up * mainThrust);
    52.             rigidBody.AddForce(Vector3.up * upThrust);
    53.            
    54.         }
    55.                
    56.     }
    57.     private void RotateShip()
    58.     {
    59.         rigidBody.freezeRotation = true;
    60.  
    61.  
    62.         float rotationThisFrame = rcsThrust * Time.deltaTime;
    63.  
    64.         if (Input.GetKey(KeyCode.A))
    65.         {
    66.  
    67.             transform.Rotate(Vector3.forward * rotationThisFrame);
    68.  
    69.         }
    70.         else if (Input.GetKey(KeyCode.D))
    71.         {
    72.             transform.Rotate(-Vector3.forward * rotationThisFrame);
    73.  
    74.         }
    75.  
    76.         rigidBody.freezeRotation = false;
    77.     }
    78.  
    79.  
    80.     void OnCollisionEnter(Collision collision)
    81.     {
    82.  
    83.         if (state != State.alive)
    84.  
    85.         {
    86.             return;
    87.         }
    88.  
    89.  
    90.         switch (collision.gameObject.tag)
    91.         {
    92.             case "Friendly":
    93.                 //do nothing
    94.                 break;
    95.  
    96.             case "Finish":
    97.                 state = State.transcending;
    98.                 Invoke("LoadNextScene", 5f);
    99.                 break;
    100.  
    101.             case "Untagged":
    102.                 state = State.dying;
    103.                 //audio.PlayOneShot(Die);    //also if I try to do it from here, it doesn't work
    104.                 Invoke("LoadStartScene", 5f);
    105.                 break;
    106.  
    107.  
    108.         }
    109.     }
    110.  
    111.     private void LoadStartScene()
    112.     {
    113.         SceneManager.LoadScene(0);
    114.     }
    115.  
    116.     private void LoadNextScene()
    117.     {
    118.         SceneManager.LoadScene(1);
    119.     }
    120.  
    121.  
    122.  
    123.    
    124.    
    125.    
    126.    
    127.    
    128.     bool hasPlayedDyingSound = false;
    129.     void Audio()
    130.     {
    131.         if (Input.GetKey(KeyCode.Space) && state == State.alive && !audio.isPlaying)
    132.         {
    133.             print("playengine");
    134.             audio.PlayOneShot(Engine);
    135.         }
    136.  
    137.          if (Input.GetKeyUp(KeyCode.Space) ||  state == State.dying || state == State.transcending)
    138.         {
    139.             audio.Stop();
    140.         }
    141.  
    142.  
    143.  
    144.  
    145.  
    146.         if (state == State.dying && !audio.isPlaying && !hasPlayedDyingSound)
    147.         {
    148.            
    149.             print("playsound");            
    150.             audio.PlayOneShot(Die);            //the problem is here, the audio is heard for just a split second, then cuts off (with any file)
    151.             hasPlayedDyingSound = true;
    152.            
    153.         }
    154.  
    155.         if (Input.GetKeyDown(KeyCode.A) && state == State.alive)
    156.         {
    157.             audio.PlayOneShot(Rotate);
    158.         }
    159.  
    160.         if (Input.GetKeyDown(KeyCode.D) && state == State.alive)
    161.         {
    162.             audio.PlayOneShot(Rotate);
    163.         }
    164.  
    165.  
    166.         if (state == State.transcending && !audio.isPlaying)
    167.  
    168.         {
    169.             audio.PlayOneShot(winLevel);
    170.                      
    171.         }
    172.  
    173.  
    174.  
    175.     }
    176.  
    177. }
    178.  
     
    Last edited: Dec 9, 2019
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    When posting code, use [code ] and not [icode ]. That way we'll get syntax highlighting and line numbers which make it easier to read and talk about the code.

    Most likely, trying to put all of these sounds on the same AudioSource is a mistake. I'm guessing the reason one of your sounds is getting cut off is most likely that another sound is being played. I'd recommend you create several AudioSource components (each with their own assigned AudioClip) rather than trying to have all the clips being played on one.
     
  3. olidadda

    olidadda

    Joined:
    Jul 27, 2019
    Posts:
    37

    Ok, I fixed the code paste, thanks for pointing that out!

    I had actually already done what you suggested, by using multiple audiosources and an array, but then on the tutorial I was following (Ben Tristem & co) I wanted to learn this procedure... seems not to work for me though: here is a link to the original code of the tutorial (under assets (rocket.cs): https://github.com/CompleteUnityDev...mmit/2213cedfb9347b2891f8bbb156a29c02b67f00a8