英文:
I'm using FMOD to create car engine sound with Unity but it didn't work, can you point out what went wrong?
问题
I started using FMOD and Mathf.Lerp to create a sound engine for my game in Unity.
我开始使用FMOD和Mathf.Lerp来为我的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 game object.
我正在使用documents/Fmod VEHICLE中的示例,并为我的汽车使用自定义物理引擎。当我运行游戏时,声音无法正常工作,并且存在与游戏对象中的fmod studio事件发射器和监听器有关的问题。
Actually, I'm 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!
实际上,我正在使用并遵循这个链接https://www.fmod.com/docs/2.02/unity/integration-tutorial.html,并且我对FMOD和Unity都是新手,可以说是五五开!
Can you please help me to run this and make it work with Max rpm and min and speed of car?
你能帮我运行这个并使其与汽车的最大RPM、最小RPM和速度一起工作吗?
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 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.
答案1
得分: 1
首先,在 Awake 函数中保存你的 CPU 循环并获取发射器,将其存储为成员。
var emitter = GetComponent<FMODUnity.StudioEventEmitter>()
现在,回答你的最初问题,你的 rezx.CurrentSpeed
不是 Lerp
函数的良好输入,因为它寻找一个在 0 - 1 范围内的值。如果你将其与 RPM 关联起来,那就意味着你需要为齿轮添加一个基本的实现。
[Serializable]
public class Gear {
public float minSpeed;
public float maxSpeed;
}
最小速度和最大速度将允许你使用 CurrentSpeed
来计算 0 - 1 范围。
有了这个声明的类,你可以在你的 ArcadeEngineAudio
类中添加 public List<Gear> gears;
,允许你在组件的 Inspector 中创建新的齿轮。
添加了齿轮之后,你可以使用 rezx.CurrentSpeed
来跟踪基于速度当前位于哪个齿轮。
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);
}
英文:
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);
}
答案2
得分: 0
我在齿轮中做了这个,如果我移动以下部分
var emitter = GetComponent<FMODUnity.StudioEventEmitter>();
到Awake
函数中,会引发错误,所以我将它保留在Update
函数中,这样错误消失了。
然而,声音不起作用。
齿轮在切换,车在当前齿轮下工作,但声音没有播放。
我是不是忘了做什么事情?
这是我的工作:
英文:
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:
答案3
得分: -1
您好,我尝试了您的代码,控制台显示了ArgumentOutOfRangeException(索引超出范围)。必须是非负数且小于集合大小。
我在控制台中修复了出现的错误,需要一个gearindex整数。
我是否漏掉了什么或者代码有问题,谢谢您的建议,丹先生。
迄今为止,我无法解决这个问题,这是全部代码:
[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);
}
}
<details>
<summary>英文:</summary>
[enter image description here][1]
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);
}
}
[1]: https://i.stack.imgur.com/TwxE1.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论