Search Unity

Audio Attaching Tachometer to Car Audio

Discussion in 'Audio & Video' started by PotooGryphon, Jul 5, 2018.

  1. PotooGryphon

    PotooGryphon

    Joined:
    Jul 5, 2018
    Posts:
    2
    Hello, (first time poster :wave: )

    So I'm still a new at C# scripting however I'm doing my best to wrap my head around it. I've imported the Vehicles Standard asset into my scene, and I have a 2D GUI Tachometer gauge (I also have a speedometer that's attached to the car's movement that's working perfectly so don't need to touch that one). Picture below for what's in my scene and what it looks like:

    upload_2018-7-5_9-40-36.png

    I'm currently trying to figure out how to attach the z rotation of the TachometerNeedle (child of the Tachometer) to the audio pitches in the CarAudio script (script applied to the car game object).

    I'm thinking the best way to go about it, is to look at my Speedometer as an example of how to drive the z rotation of the gauge needle. For the Speedometer, I have a script applied to the needle (AnalogueSpeedConverter thanks to Holistic3d tut) and added a snippet of code in the CarUserController script to replace the PlayerController script in Holistic 3d's tut.

    Gauge Needle Script:
    Code (CSharp):
    1. public class AnalogueSpeedConverter : MonoBehaviour {
    2.  
    3.     static float minAngle = 0.0f;
    4.     static float maxAngle = -240.0f;
    5.     static AnalogueSpeedConverter thisSpeedo;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         thisSpeedo = this;
    10.        
    11.     }
    12.  
    13.     public static void ShowSpeed(float speed, float min, float max)
    14.     {
    15.         float ang = Mathf.Lerp(minAngle, maxAngle, Mathf.InverseLerp(min, max, speed));
    16.         thisSpeedo.transform.eulerAngles = new Vector3(0, 0, ang);
    17.     }
    18. }
    Snippet added to the CarUserControl Script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityStandardAssets.CrossPlatformInput;
    3.  
    4. namespace UnityStandardAssets.Vehicles.Car
    5. {
    6.     [RequireComponent(typeof (CarController))]
    7.     public class CarUserControl : MonoBehaviour
    8.     {
    9.         private CarController m_Car; // the car controller we want to use
    10.  
    11.        //added Rigidbody variable to script
    12.         Rigidbody rb;
    13.  
    14.         private void Awake()
    15.         {
    16.             // get the car controller
    17.             m_Car = GetComponent<CarController>();
    18.  
    19.             //added to script
    20.             rb = this.GetComponent<Rigidbody>();
    21.         }
    22.  
    23.  
    24.         private void FixedUpdate()
    25.         {
    26.             // pass the input to the car!
    27.             float h = CrossPlatformInputManager.GetAxis("Horizontal");
    28.             float v = CrossPlatformInputManager.GetAxis("Vertical");
    29. #if !MOBILE_INPUT
    30.             float handbrake = CrossPlatformInputManager.GetAxis("Jump");
    31.             m_Car.Move(h, v, v, handbrake);
    32. #else
    33.             m_Car.Move(h, v, v, 0f);
    34. #endif
    35.         }
    36.      
    37.         // Update is called once per frame
    38.         void Update()
    39.         {
    40.             //added to script
    41.             AnalogueSpeedConverter.ShowSpeed(rb.velocity.magnitude, 0, 100);
    42.         }
    43.  
    44.     }
    45. }
    In the CarAudio script, I'm thinking the best place to look to for where gears are shifting, for the Tachometer gauge to read, is where the pitch is being applied or perhaps the volume(?) There are two floats setup at the start; lowPitchMin = 1f; and lowPitchMax = 6f; Perhaps I can have the z rotation on the needle rotate towards the maxAngle as the pitch raises towards the lowPitchMax and have the rotation drop as the audio shifts back towards lowPitchMin?

    Currently I'm a bit unsure how to set this up and would love some guidance to bounce off of to think this through.

    CarAudio script that deals with the audio pitch/volume:
    Code (CSharp):
    1.      if (m_StartedSound)
    2.             {
    3.                 // The pitch is interpolated between the min and max values, according to the car's revs.
    4.                 float pitch = ULerp(lowPitchMin, lowPitchMax, m_CarController.Revs);
    5.  
    6.                 // clamp to minimum pitch (note, not clamped to max for high revs while burning out)
    7.                 pitch = Mathf.Min(lowPitchMax, pitch);
    8.  
    9.                 if (engineSoundStyle == EngineAudioOptions.Simple)
    10.                 {
    11.                     // for 1 channel engine sound, it's oh so simple:
    12.                     m_HighAccel.pitch = pitch*pitchMultiplier*highPitchMultiplier;
    13.                     m_HighAccel.dopplerLevel = useDoppler ? dopplerLevel : 0;
    14.                     m_HighAccel.volume = 1;
    15.                 }
    16.                 else
    17.                 {
    18.                     // for 4 channel engine sound, it's a little more complex:
    19.  
    20.                     // adjust the pitches based on the multipliers
    21.                     m_LowAccel.pitch = pitch*pitchMultiplier;
    22.                     m_LowDecel.pitch = pitch*pitchMultiplier;
    23.                     m_HighAccel.pitch = pitch*highPitchMultiplier*pitchMultiplier;
    24.                     m_HighDecel.pitch = pitch*highPitchMultiplier*pitchMultiplier;
    25.  
    26.                     // get values for fading the sounds based on the acceleration
    27.                     float accFade = Mathf.Abs(m_CarController.AccelInput);
    28.                     float decFade = 1 - accFade;
    29.  
    30.                     // get the high fade value based on the cars revs
    31.                     float highFade = Mathf.InverseLerp(0.2f, 0.8f, m_CarController.Revs);
    32.                     float lowFade = 1 - highFade;
    33.  
    34.                     // adjust the values to be more realistic
    35.                     highFade = 1 - ((1 - highFade)*(1 - highFade));
    36.                     lowFade = 1 - ((1 - lowFade)*(1 - lowFade));
    37.                     accFade = 1 - ((1 - accFade)*(1 - accFade));
    38.                     decFade = 1 - ((1 - decFade)*(1 - decFade));
    39.  
    40.                     // adjust the source volumes based on the fade values
    41.                     m_LowAccel.volume = lowFade*accFade;
    42.                     m_LowDecel.volume = lowFade*decFade;
    43.                     m_HighAccel.volume = highFade*accFade;
    44.                     m_HighDecel.volume = highFade*decFade;
    45.  
    46.                     // adjust the doppler levels
    47.                     m_HighAccel.dopplerLevel = useDoppler ? dopplerLevel : 0;
    48.                     m_LowAccel.dopplerLevel = useDoppler ? dopplerLevel : 0;
    49.                     m_HighDecel.dopplerLevel = useDoppler ? dopplerLevel : 0;
    50.                     m_LowDecel.dopplerLevel = useDoppler ? dopplerLevel : 0;
    51.                 }
    My best guess for how to code it:

    Code on the TachomtereNeedle to setup the minAngle, maxAngle, and a formula to change the z rotation within parameters:

    Code (CSharp):
    1. public class TachGauge : MonoBehaviour {
    2.  
    3.     static float minAngle = 0.0f;
    4.     static float maxAngle = -120.0f;
    5.     static TachGauge thisRPM;
    6.  
    7.     // Use this for initialization
    8.     void Start()
    9.     {
    10.         thisRPM = this;
    11.  
    12.     }
    13.  
    14.     public static void ShowSpeed(float speed, float min, float max)
    15.     {
    16.         float ang = Mathf.Lerp(minAngle, maxAngle, Mathf.InverseLerp(min, max, speed));
    17.         thisRPM.transform.eulerAngles = new Vector3(0, 0, ang);
    18.     }
    19. }
    Code added in the CarAudio script at the end of the private void Update();
    Code (CSharp):
    1.                 RPMGauge.ShowSpeed(m_HighAccel.volume, 0, 100);
    Upon, testing this the needle is unresponsive. Please any help with figuring this out would be helpful. I'm currently in the process of learning.

    Thank you!
     

    Attached Files:

  2. PotooGryphon

    PotooGryphon

    Joined:
    Jul 5, 2018
    Posts:
    2
    (Update)

    [thinking out loud]
    Perhaps attaching the rotation to the gears changes in the CarController, would be better?

    I've deleted my addition in the CarAudio script and added a line in the private void GearChanging() function in the CarController script.

    Code (CSharp):
    1.         private void GearChanging()
    2.         {
    3.             float f = Mathf.Abs(CurrentSpeed/MaxSpeed);
    4.             float upgearlimit = (1/(float) NoOfGears)*(m_GearNum + 1);
    5.             float downgearlimit = (1/(float) NoOfGears)*m_GearNum;
    6.  
    7.             if (m_GearNum > 0 && f < downgearlimit)
    8.             {
    9.                 m_GearNum--;
    10.             }
    11.  
    12.             if (f > upgearlimit && (m_GearNum < (NoOfGears - 1)))
    13.             {
    14.                 m_GearNum++;
    15.             }
    16.  
    17.             //added line of code
    18.             RMPGauge.ShowSpeed((m_GearNum * Time.deltaTime)*100, 0, 100);
    19.         }
    It does seem to work, however the isn't very smooth and the rotation amount isn't very much. I've had to multiple by 100 just to see it function.

    Any suggestions?
     
  3. Docaroo

    Docaroo

    Joined:
    Nov 7, 2017
    Posts:
    82
    I think you'll have better luck over in the code forums and for googling for "Unity Tachometer" ... the audio aspect of this is really quite straightforward once you have the code working right.