-
Notifications
You must be signed in to change notification settings - Fork 0
/
REEngine.cs
178 lines (154 loc) · 4.5 KB
/
REEngine.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using UnityEngine;
public class REEngine : MonoBehaviour
{
public int maxGear = 5;
public float maxSteer = 25.0f;
public float enginePower = 150.0f;
public int idleRPM = 1100;
public int dropRPM = 2200;
public int maxRPM = 6500;
public float finalDrive = 4.0f;
public AnimationCurve powerCurve;
public AnimationCurve differential;
private float _power;
private float _brake;
private float _steer;
private bool _handbrake;
private float _wheelRPM;
private int RPM { get; set; }
private int _currentGear;
private AudioSource _engineSound;
private void Awake()
{
_engineSound = GetComponent<AudioSource>();
}
public float GetPowerTorque()
{
return _power;
}
public float GetBrakeTorque()
{
return _brake;
}
public float GetSteerAngle()
{
return _steer;
}
public bool GetHandBrake()
{
return _handbrake;
}
public void SetGearToIdle()
{
_currentGear = 0;
}
public void SetGearOn()
{
_currentGear = 1;
}
public int GetRPM()
{
return RPM;
}
public int GetGear()
{
return _currentGear;
}
public void OnUpdate(
float carMass,
float wheelRatio,
float FLRPM,
float FRRPM,
float RLRPM,
float RRRPM)
{
_handbrake = Input.GetButton("R1");
_steer = Input.GetAxis("LX") * maxSteer;
var accelerationInput = Input.GetButton("R2") ? 1.0f : 0.0f;
var deAccelerationInput = Input.GetButton("L2") ? -1.0f : 0.0f;
var acceleration = accelerationInput + deAccelerationInput;
if (!RERace.IsRacing())
{
accelerationInput = 0.0f;
deAccelerationInput = 0.0f;
acceleration = 0.0f;
}
var speed = transform.parent.GetComponent<Rigidbody>().velocity.magnitude;
if (acceleration > 0.0f)
{
if (_currentGear <= 0)
{
_currentGear++;
//audioeffect.Play();
}
else if (_currentGear < maxGear)
{
if (RPM > 0.85f * maxRPM)
{
_currentGear++;
//audioeffect.Play();
}
}
}
else if (acceleration < 0.0f)
{
if (_currentGear == 0 && speed < 1.5f)
{
_currentGear--;
//audioeffect.Play();
}
else if (RPM < dropRPM && _currentGear > 0)
{
_currentGear--;
//audioeffect.Play();
}
}
else
{
if (_currentGear > 0)
{
if (RPM < dropRPM)
{
_currentGear--;
//audioeffect.Play();
}
}
}
/*OLD manual shifting, likely not needed
else
{
if( currentGear<maxGear )
if( Input.GetKeyUp(
KeyCode.E ) )
currentGear++;
if( currentGear>1 )
if( Input.GetKeyUp(
KeyCode.Q ) )
currentGear--;
}*/
var wheelRadius = 0.3f;
var wheelCircumference = wheelRadius * 2.0f * Mathf.PI;
var effectiveGearRatio = finalDrive * differential.Evaluate(_currentGear);
_wheelRPM = ((FLRPM + FRRPM)/2 * (1.0f - wheelRatio) + (RLRPM + RRRPM)/2 * wheelRatio) * effectiveGearRatio;
RPM = Mathf.RoundToInt(speed / (wheelCircumference / 60) * effectiveGearRatio);//Mathf.RoundToInt(Mathf.Abs(_wheelRPM));//Mathf.RoundToInt(speed / (wheelCircumference / 60) * effectiveGearRatio); //Mathf.RoundToInt(_wheelRPM);
RPM = Mathf.Max(idleRPM, RPM);
var scaledRPM = (RPM - idleRPM) / (float) (maxRPM - idleRPM);
_engineSound.pitch = 1.0f + scaledRPM;
var engineOutput = powerCurve.Evaluate(RPM / 10000.0f) * enginePower * Mathf.Abs(acceleration);
_power = engineOutput * effectiveGearRatio;
if (deAccelerationInput < 0.0f && _currentGear >= 0)
{
_brake = carMass * 1.3f * 9.81f;
//_power = 0.0f;
}
else if (accelerationInput > 0.0f && _currentGear == -1)
{
_brake = carMass * 1.3f * 9.81f;
//_power = 0.0f;
}
else
{
_brake = _handbrake ? carMass * 9.81f : 0.0f;
}
}
}