-
Notifications
You must be signed in to change notification settings - Fork 0
/
RERollBar.cs
55 lines (45 loc) · 1.39 KB
/
RERollBar.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
using UnityEngine;
public class RERollBar : MonoBehaviour
{
public REWheel leftWheel;
public REWheel rightWheel;
public float antiRoll = 5000.0f;
public void OnUpdate(
Rigidbody carBody,
float powerTorque,
float brakeTorque,
float steerAmount,
bool applyHandBrake)
{
leftWheel.OnUpdate(
powerTorque / 2.0f,
brakeTorque,
steerAmount,
applyHandBrake);
rightWheel.OnUpdate(
powerTorque / 2.0f,
brakeTorque,
steerAmount,
applyHandBrake);
var groundedL = leftWheel.IsGrounded();
var groundedR = rightWheel.IsGrounded();
if (!groundedL && !groundedR)
return;
var travelL = groundedL ? leftWheel.GetSuspensionTravel() : 0.5f;
var travelR = groundedR ? rightWheel.GetSuspensionTravel() : 0.5f;
var antiRollForce = (travelL - travelR) * antiRoll;
if (groundedL)
{
var lwTransform = leftWheel.transform;
carBody.AddForceAtPosition(
lwTransform.up * -antiRollForce,
lwTransform.position);
}
if (!groundedR)
return;
var rwTransform = rightWheel.transform;
carBody.AddForceAtPosition(
rwTransform.up * antiRollForce,
rwTransform.position);
}
}