-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCC_Chassis.cs
More file actions
156 lines (105 loc) · 4.46 KB
/
RCC_Chassis.cs
File metadata and controls
156 lines (105 loc) · 4.46 KB
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
//----------------------------------------------
// Realistic Car Controller
//
// Copyright © 2014 - 2017 BoneCracker Games
// http://www.bonecrackergames.com
// Buğra Özdoğanlar
//
//----------------------------------------------
using UnityEngine;
using System.Collections;
/// <summary>
/// Simulates chassis movement based on vehicle rigidbody velocity.
/// </summary>
[AddComponentMenu("BoneCracker Games/Realistic Car Controller/Misc/RCC Chassis")]
public class RCC_Chassis : MonoBehaviour {
// Getting an Instance of Main Shared RCC Settings.
#region RCC Settings Instance
private RCC_Settings RCCSettingsInstance;
private RCC_Settings RCCSettings {
get {
if (RCCSettingsInstance == null) {
RCCSettingsInstance = RCC_Settings.Instance;
}
return RCCSettingsInstance;
}
}
#endregion
private RCC_CarControllerV3 carController;
private Rigidbody rigid;
private float chassisVerticalLean = 4f; // Chassis Vertical Lean Sensitivity.
private float chassisHorizontalLean = 4f; // Chassis Horizontal Lean Sensitivity.
private float horizontalLean = 0f;
private float verticalLean = 0f;
void Start () {
carController = GetComponentInParent<RCC_CarControllerV3> ();
rigid = carController.GetComponent<Rigidbody> ();
if (!RCCSettings.dontUseChassisJoint)
ChassisJoint ();
}
void OnEnable(){
if (!RCCSettings.dontUseChassisJoint)
StartCoroutine ("ReEnable");
}
IEnumerator ReEnable(){
if (!transform.parent.GetComponent<ConfigurableJoint> ())
yield break;
GameObject _joint = GetComponentInParent<ConfigurableJoint>().gameObject;
_joint.GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.None;
yield return new WaitForFixedUpdate();
_joint.GetComponent<Rigidbody>().interpolation = RigidbodyInterpolation.Interpolate;
}
void ChassisJoint(){
GameObject colliders = new GameObject("Colliders");
colliders.transform.SetParent(GetComponentInParent<RCC_CarControllerV3> ().transform, false);
GameObject chassisJoint;
Transform[] childTransforms = GetComponentInParent<RCC_CarControllerV3> ().chassis.GetComponentsInChildren<Transform>();
foreach(Transform t in childTransforms){
if(t.gameObject.activeSelf && t.GetComponent<Collider>()){
if (t.childCount >= 1) {
Transform[] childObjects = t.GetComponentsInChildren<Transform> ();
foreach (Transform c in childObjects) {
if (c != t) {
c.SetParent (transform);
}
}
}
GameObject newGO = (GameObject)Instantiate(t.gameObject, t.transform.position, t.transform.rotation);
newGO.transform.SetParent(colliders.transform, true);
newGO.transform.localScale = t.lossyScale;
Component[] components = newGO.GetComponents(typeof(Component));
foreach(Component comp in components){
if(!(comp is Transform) && !(comp is Collider)){
Destroy(comp);
}
}
}
}
chassisJoint = (GameObject)Instantiate((RCCSettings.chassisJoint), Vector3.zero, Quaternion.identity);
chassisJoint.transform.SetParent(rigid.transform, false);
chassisJoint.GetComponent<ConfigurableJoint> ().connectedBody = rigid;
chassisJoint.GetComponent<ConfigurableJoint> ().autoConfigureConnectedAnchor = false;
transform.SetParent(chassisJoint.transform, false);
Collider[] collidersInChassis = GetComponentsInChildren<Collider>();
foreach(Collider c in collidersInChassis)
Destroy(c);
GetComponentInParent<Rigidbody> ().centerOfMass = new Vector3 (rigid.centerOfMass.x, rigid.centerOfMass.y + 1f, rigid.centerOfMass.z);
}
void Update(){
chassisVerticalLean = carController.chassisVerticalLean;
chassisHorizontalLean = carController.chassisHorizontalLean;
}
void FixedUpdate () {
if (RCCSettings.dontUseChassisJoint)
LegacyChassis ();
}
void LegacyChassis (){
Vector3 localVelocity = rigid.transform.InverseTransformDirection (rigid.angularVelocity);
verticalLean = Mathf.Clamp(Mathf.Lerp (verticalLean, rigid.angularVelocity.x * chassisVerticalLean, Time.fixedDeltaTime * 5f), -5f, 5f);
horizontalLean = Mathf.Clamp(Mathf.Lerp (horizontalLean, localVelocity.y * chassisHorizontalLean, Time.fixedDeltaTime * 5f), -5f, 5f);
if(float.IsNaN(verticalLean) || float.IsNaN(horizontalLean) || float.IsInfinity(verticalLean) || float.IsInfinity(horizontalLean) || Mathf.Approximately(verticalLean, 0f) || Mathf.Approximately(horizontalLean, 0f))
return;
Quaternion target = Quaternion.Euler(verticalLean, transform.localRotation.y, horizontalLean);
transform.localRotation = target;
}
}