Search Unity

Gear shift sound.

Discussion in 'Scripting' started by autobotlongarm, May 5, 2018.

  1. autobotlongarm

    autobotlongarm

    Joined:
    Jan 1, 2018
    Posts:
    19
    So I have a part of the script here since it's long. I want to add shift sounds when the player goes up or down a gear, problem is I don't know how to make it play.
    Here's a part of it

    Code (CSharp):
    1.  void ShiftGears()
    2.         {
    3.             //Shift Down
    4.             if(currentSpeed >= gearSpeeds[currentGear] && FL_WheelCollider.rpm > 0)
    5.             {
    6.                 currentGear ++;
    7.             }
    8.  
    9.             //Shift Up
    10.             if (currentSpeed < gearSpeeds[currentGear - 1])
    11.             {
    12.                 currentGear--;
    13.             }
    14.  
    15.             EngineSound();    
    16.         }
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Create a public AudioSource variable:
    Code (CSharp):
    1. public AudioSource gearShiftAudioSource;
    Create an AudioSource in the Editor and make it a child of the car. Assign the gear shift sound clip to the AudioSource then drag and drop the AudioSource in the newly created "gearShiftAudioSource" slot in the inspector.

    Other ways to go about this is to have a function that creates the AudioSource at runtime.

    In the ShiftGears() function, Play the AudioSource:
    Code (CSharp):
    1. if(currentSpeed >= gearSpeeds[currentGear] && FL_WheelCollider.rpm > 0)
    2. {
    3.     currentGear ++;
    4.     gearShiftAudioSource.Play();
    5. }
    6. if (currentSpeed < gearSpeeds[currentGear - 1])
    7. {
    8.     currentGear--;
    9.     gearShiftAudioSource.Play();
    10. }
     
  3. autobotlongarm

    autobotlongarm

    Joined:
    Jan 1, 2018
    Posts:
    19
    It doesn't work, it keeps saying it hasn't been assigned.
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Have you assigned your sound clip? Do you have a sound clip? :)
     
  5. autobotlongarm

    autobotlongarm

    Joined:
    Jan 1, 2018
    Posts:
    19

    In the gearShift, I have the Audio Source with the audio I want it to play.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  7. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry the image is covered by a black box
     
    xVergilx likes this.
  8. autobotlongarm

    autobotlongarm

    Joined:
    Jan 1, 2018
    Posts:
    19
    I can't find it in the inspector. Even when I went back to add the clips and audiosource, the button didn't show up in the inspector. Would I have to restart all of my cars so I could get the button to show up or would I need to edit it with something else cause I'm using Notepad instead of Monoeditor.
     
  9. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Why u using notepad man?

    Did you declare them as public or serialized?
     
  10. autobotlongarm

    autobotlongarm

    Joined:
    Jan 1, 2018
    Posts:
    19
    No, cause I do not know how to do that at all.
     
  11. autobotlongarm

    autobotlongarm

    Joined:
    Jan 1, 2018
    Posts:
    19
    So I used VC and it couldn't find the varible. So I went in and wanted to do this
    Code (CSharp):
    1.           //Shift Down
    2.             if (currentSpeed >= gearSpeeds[currentGear] && FL_WheelCollider.rpm > 0)
    3.             {
    4.                 currentGear++;
    5.                 NewMethod();
    6.             }
    7.             if (currentSpeed < gearSpeeds[currentGear - 1])
    8.             {
    9.                 currentGear--;
    10.                 NewMethod1();
    11.             }
     
  12. autobotlongarm

    autobotlongarm

    Joined:
    Jan 1, 2018
    Posts:
    19