0

I started using FMOD and Mathf.Lerp to create a sound engine for my game in Unity.

I'm using the examples in documents/Fmod VEHICLE and using custom physics for my car. When I run the game the audio doesn't work, and problem with fmod studio event emitter and listener in gameobject.

Actually im using and following this https://www.fmod.com/docs/2.02/unity/integration-tutorial.html and I am new to FMOD and Unity, 50-50 you can say!

Can you please help me to run this and make it work with Max rpm and min and speed of car

This is the code I tried which has some issue:

using UnityEngine;

    public class ArcadeEngineAudio : MonoBehaviour
    {
        PG.CarController rezx;
        
        void Awake()
        {
            rezx = GetComponentInParent<PG.CarController>();
        }
        
        void Update()
        {

            float effectiveRPM = Mathf.Lerp(rezx.MinRPM, rezx.MaxRPM, rezx.CurrentSpeed);
            var emitter = GetComponent<FMODUnity.StudioEventEmitter>();
            emitter.SetParameter("RPM", effectiveRPM);
        }
    }

Thanks in advance.

I tried to use engine rpm, min rpm, and max rpm which doesn't work, even the current speed.

3 Answers 3

1

First off, save your CPU cycles and grab the emitter in the Awake function, storing it as a member.

var emitter = GetComponent<FMODUnity.StudioEventEmitter>()

Now, to answer your original question, your rezx.CurrentSpeed isn't a good input for the Lerp function as that is looking for a value in a 0 - 1 range. If you're tying it to RPM then that suggests you will need to add a basic implementation for gears.

    [Serializable]
    public class Gear {

      public float minSpeed;
      public float maxSpeed;

    }

A min and max speed will allow you to calculate that 0 - 1 range with CurrentSpeed.

With this class declared, you can add public List<Gear> gears; to your ArcadeEngineAudio class, allowing you to create new gears in the Inspector of the component.

Once you have added your gears, you can use the rezx.CurrentSpeed to track which gear you're currently in based on the speed.

    void Update() {
        
        if(rezx.CurrentSpeed > gears[gearIndex].maxSpeed && gearIndex - 1 < gears.Count)
            gearIndex++;
            
        if(rezx.CurrentSpeed < gears[gearIndex].minSpeed && gearIndex > 0)
            gearIndex--;
        
        var min = gears[gearIndex].minSpeed;
        var max = gears[gearIndex].maxSpeed;
        
        var amount = (rezx.CurrentSpeed - min) / (max - min)
        
        float rpm = Mathf.Lerp(rezx.MinRPM, rezx.MaxRPM, amount);
        emitter.SetParameter("RPM", rpm);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

You need to cap gearIndex to between 0 and the size of your list, I'll edit the snippet to account for this.
0

I DID this in gears and if i move the following

 var emitter = GetComponent<FMODUnity.StudioEventEmitter>();

to the Awake function it throws an error, so I kept it in the Update function, which made the error go away. However, the sounds don't work. The gears are switching and the car works in the current gear but sounds are not playing.

Did I forget something to do? Here is my work:

https://prnt.sc/-xMM39bM4SNw

https://prnt.sc/HaTexyaBSrjA

https://prnt.sc/xAn6vpcLE8tU

https://prnt.sc/ARcbBkP9OQaf

https://prnt.sc/z6-tJIM46Jou

2 Comments

any replay? bro
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-1

enter image description here

Hello I Tried ur code and the console showing ArgumentOutOfRangeException Index was out of range. Must be non-negative and less than the size of the collection.

i fixed the errors comes to me in console needs gearindex int

did i missing something or the code wrong thanks in advice mr dan

icant solve the problem yet this all the code

    [Serializable]
public class Gear {

  public float minSpeed;
  public float maxSpeed;

}

public class ArcadeEngineAudio : MonoBehaviour
{
    PG.CarController uvc;
    public List<Gear> gears;
    public int gearIndex;

    void Awake()
    {
        uvc = GetComponentInParent<PG.CarController>();
    }
        
     void Update() {
    
    if(uvc.CurrentSpeed > gears[gearIndex].maxSpeed)
        gearIndex++;
        
    if(uvc.CurrentSpeed < gears[gearIndex].minSpeed)
        gearIndex--;
    
    var min = gears[gearIndex].minSpeed;
    var max = gears[gearIndex].maxSpeed;
    
    var amount = (uvc.CurrentSpeed - min) / (max - min);
    var emitter = GetComponent<FMODUnity.StudioEventEmitter>();

    float rpm = Mathf.Lerp(uvc.MinRPM, uvc.MaxRPM, amount);
    emitter.SetParameter("RPM", rpm);
    }
}

2 Comments

I've altered the first snippet to prevent your error. For the gears in the inspector, you need to add a minimum and maximum speed for each gear. So say you have a car with 5 gears: Index 0: { MinSpeed = 0 MaxSpeed = 10 } Index 1: { MinSpeed = 11 MaxSpeed = 20 } Index 2: { MinSpeed = 21 MaxSpeed = 30 } Index 3: { MinSpeed = 31 MaxSpeed = 40 } Index 4: { MinSpeed = 41 MaxSpeed = 50 }
Should rather be a comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.