forked from Nanousis/GearSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEngineAudio.cs
78 lines (73 loc) · 2.41 KB
/
EngineAudio.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EngineAudio : MonoBehaviour
{
public AudioSource runningSound;
public float runningMaxVolume;
public float runningMaxPitch;
public AudioSource reverseSound;
public float reverseMaxVolume;
public float reverseMaxPitch;
public AudioSource idleSound;
public float idleMaxVolume;
public float speedRatio;
private float revLimiter;
public float LimiterSound = 1f;
public float LimiterFrequency = 3f;
public float LimiterEngage = 0.8f;
public bool isEngineRunning = false;
public AudioSource startingSound;
private CarController carController;
// Start is called before the first frame update
void Start()
{
carController = GetComponent<CarController>();
idleSound.volume = 0;
runningSound.volume = 0;
reverseSound.volume = 0;
}
// Update is called once per frame
void Update()
{
float speedSign=0;
if (carController)
{
speedSign = Mathf.Sign(carController.GetSpeedRatio());
speedRatio = Mathf.Abs(carController.GetSpeedRatio());
}
if (speedRatio > LimiterEngage)
{
revLimiter = (Mathf.Sin(Time.time * LimiterFrequency) + 1f) * LimiterSound * (speedRatio - LimiterEngage);
}
if (isEngineRunning)
{
idleSound.volume = Mathf.Lerp(0.1f, idleMaxVolume, speedRatio);
if (speedSign > 0)
{
reverseSound.volume = 0;
runningSound.volume = Mathf.Lerp(0.3f, runningMaxVolume, speedRatio);
runningSound.pitch = Mathf.Lerp(0.3f, runningMaxPitch, speedRatio);
}
else
{
runningSound.volume = 0;
reverseSound.volume = Mathf.Lerp(0f, reverseMaxVolume, speedRatio);
reverseSound.pitch = Mathf.Lerp(0.2f, reverseMaxPitch, speedRatio);
}
}
else {
idleSound.volume = 0;
runningSound.volume = 0;
}
}
public IEnumerator StartEngine()
{
startingSound.Play();
carController.isEngineRunning = 1;
yield return new WaitForSeconds(0.6f);
isEngineRunning = true;
yield return new WaitForSeconds(0.4f);
carController.isEngineRunning = 2;
}
}