1
+ using System ;
2
+ using System . Linq ;
3
+ using Unity . Collections ;
4
+ using Unity . Entities ;
5
+ using Unity . Mathematics ;
6
+ using UnityEngine ;
7
+ using Unity . Entities . Racing . Common ;
8
+
9
+ namespace Dots . Racing
10
+ {
11
+ public class VehicleAuthoring : MonoBehaviour
12
+ {
13
+ [ Header ( "Suspension" ) ] public float RestLength = 0.45f ;
14
+ public float SpringStiffness ;
15
+ public float DamperStiffness ;
16
+
17
+ [ Header ( "Steering" ) ]
18
+ public float TurnRadius ;
19
+ public float SteeringForce = 2 ;
20
+ public float SteeringTime = 10 ;
21
+
22
+ [ Header ( "AntiRollBar" ) ]
23
+ public float AntiRollStiffness = 5000 ;
24
+ public float DownForce = 50 ;
25
+
26
+ [ Header ( "Acceleration" ) ]
27
+ public float DriveTorque = 50 ;
28
+ public float MaxDriveTorque = 70 ;
29
+ public AnimationCurve DriveTorqueCurve = new ( ) ;
30
+ public float BreakTorque = 50f ;
31
+ public CollisionCategories BodyCollisionMask ;
32
+
33
+ [ Header ( "Wheels" ) ]
34
+ public float WheelsRadius ;
35
+ public CollisionCategories WheelsCollisionMask ;
36
+ public WheelAuthoringHelper [ ] Wheels ;
37
+
38
+ [ Header ( "Engine Sound" ) ]
39
+ public float MinAudioVolume = 0.4f ;
40
+ public float MaxAudioVolume = 1.0f ;
41
+ [ Serializable ]
42
+ public class WheelAuthoringHelper
43
+ {
44
+ public GameObject WheelSpring ;
45
+ public GameObject WheelMesh ;
46
+ public WheelPlacement Placement ;
47
+ public bool IsSteering ;
48
+ [ Range ( 0 , 1 ) ]
49
+ public float GripFactor = 0.3f ;
50
+ }
51
+
52
+ private void OnDrawGizmos ( )
53
+ {
54
+ foreach ( var wheel in Wheels )
55
+ {
56
+ var pos = wheel . WheelSpring . transform . position ;
57
+ var wheelCenter = pos - RestLength * Vector3 . up ;
58
+ Gizmos . color = Color . green ;
59
+ Gizmos . DrawLine ( pos , pos - RestLength * Vector3 . up ) ;
60
+
61
+ Gizmos . color = Color . gray ;
62
+ Gizmos . DrawWireSphere ( wheelCenter , WheelsRadius ) ;
63
+ }
64
+ }
65
+ }
66
+
67
+ public class VehicleBaking : Baker < VehicleAuthoring >
68
+ {
69
+ public override void Bake ( VehicleAuthoring authoring )
70
+ {
71
+ var vehicleBakingData = new VehicleBakingData
72
+ {
73
+ Authoring = authoring ,
74
+ WheelVisual0 = GetEntity ( authoring . Wheels [ 0 ] . WheelMesh ) ,
75
+ WheelVisual1 = GetEntity ( authoring . Wheels [ 1 ] . WheelMesh ) ,
76
+ WheelVisual2 = GetEntity ( authoring . Wheels [ 2 ] . WheelMesh ) ,
77
+ WheelVisual3 = GetEntity ( authoring . Wheels [ 3 ] . WheelMesh ) ,
78
+ WheelSpring0 = GetEntity ( authoring . Wheels [ 0 ] . WheelSpring ) ,
79
+ WheelSpring1 = GetEntity ( authoring . Wheels [ 1 ] . WheelSpring ) ,
80
+ WheelSpring2 = GetEntity ( authoring . Wheels [ 2 ] . WheelSpring ) ,
81
+ WheelSpring3 = GetEntity ( authoring . Wheels [ 3 ] . WheelSpring )
82
+ } ;
83
+
84
+ AddComponent ( vehicleBakingData ) ;
85
+ }
86
+ }
87
+
88
+ [ TemporaryBakingType ]
89
+ public struct VehicleBakingData : IComponentData
90
+ {
91
+ public UnityObjectRef < VehicleAuthoring > Authoring ;
92
+ public Entity WheelVisual0 ;
93
+ public Entity WheelVisual1 ;
94
+ public Entity WheelVisual2 ;
95
+ public Entity WheelVisual3 ;
96
+
97
+ public Entity WheelSpring0 ;
98
+ public Entity WheelSpring1 ;
99
+ public Entity WheelSpring2 ;
100
+ public Entity WheelSpring3 ;
101
+ }
102
+
103
+
104
+ [ WorldSystemFilter ( WorldSystemFilterFlags . BakingSystem ) ]
105
+ [ UpdateInGroup ( typeof ( PostBakingSystemGroup ) ) ]
106
+ public partial class VehicleBaker : SystemBase
107
+ {
108
+ protected override void OnUpdate ( )
109
+ {
110
+ Entities
111
+ . WithEntityQueryOptions ( EntityQueryOptions . IncludePrefab )
112
+ . WithStructuralChanges ( )
113
+ . ForEach ( ( Entity entity , ref VehicleBakingData vehicleBakingData ) =>
114
+ {
115
+ var vehicleAuthoring = vehicleBakingData . Authoring . Value ;
116
+ var chassis = new ChassisReference { Value = entity } ;
117
+ var suspension = new Suspension
118
+ {
119
+ RestLength = vehicleAuthoring . RestLength ,
120
+ SpringStiffness = vehicleAuthoring . SpringStiffness ,
121
+ DamperStiffness = vehicleAuthoring . DamperStiffness ,
122
+ SpringLength = vehicleAuthoring . RestLength
123
+
124
+ } ;
125
+ //Setup steering
126
+ var wheelsBase = GetWheelsBaseDistance ( vehicleAuthoring . Wheels ) ;
127
+ var rearTrack = GetRearTrackDistance ( vehicleAuthoring . Wheels ) ;
128
+ var antiRoll = new AntiRollBar { Stiffness = vehicleAuthoring . AntiRollStiffness } ;
129
+ var driveTorqueCurve =
130
+ AnimationCurveBlob . CreateBlob ( vehicleAuthoring . DriveTorqueCurve , Allocator . Persistent ) ;
131
+ var vehicleChassis = new VehicleChassis {
132
+ DownForce = vehicleAuthoring . DownForce ,
133
+ CollisionMask = vehicleAuthoring . BodyCollisionMask
134
+ } ;
135
+ for ( var i = 0 ; i < vehicleAuthoring . Wheels . Length ; i ++ )
136
+ {
137
+ var wheelAuthoring = vehicleAuthoring . Wheels [ i ] ;
138
+
139
+ var visualMesh = i switch
140
+ {
141
+ 0 => vehicleBakingData . WheelVisual0 ,
142
+ 1 => vehicleBakingData . WheelVisual1 ,
143
+ 2 => vehicleBakingData . WheelVisual2 ,
144
+ 3 => vehicleBakingData . WheelVisual3 ,
145
+ _ => vehicleBakingData . WheelVisual0
146
+ } ;
147
+
148
+ var wheel = new Wheel
149
+ {
150
+ CollisionMask = vehicleAuthoring . WheelsCollisionMask ,
151
+ Radius = vehicleAuthoring . WheelsRadius ,
152
+ DriveTorque = vehicleAuthoring . DriveTorque ,
153
+ MaxDriveTorque = vehicleAuthoring . MaxDriveTorque ,
154
+ DriveTorqueCurve = driveTorqueCurve ,
155
+ GripFactor = wheelAuthoring . GripFactor ,
156
+ Placement = wheelAuthoring . Placement ,
157
+ VisualMesh = visualMesh ,
158
+ } ;
159
+
160
+ var wheelEntity = i switch
161
+ {
162
+ 0 => vehicleBakingData . WheelSpring0 ,
163
+ 1 => vehicleBakingData . WheelSpring1 ,
164
+ 2 => vehicleBakingData . WheelSpring2 ,
165
+ 3 => vehicleBakingData . WheelSpring3 ,
166
+ _ => vehicleBakingData . WheelSpring0
167
+ } ;
168
+
169
+ if ( wheelAuthoring . IsSteering )
170
+ {
171
+ var steering = new Steering
172
+ {
173
+ TurnRadius = vehicleAuthoring . TurnRadius ,
174
+ SteeringForce = vehicleAuthoring . SteeringForce ,
175
+ WheelsBase = wheelsBase ,
176
+ RearTrack = rearTrack ,
177
+ SteeringTime = vehicleAuthoring . SteeringTime ,
178
+ } ;
179
+ EntityManager . AddComponentData ( wheelEntity , steering ) ;
180
+ }
181
+
182
+ switch ( wheelAuthoring . Placement )
183
+ {
184
+ case WheelPlacement . FrontRight :
185
+ antiRoll . FrontRightWheel = wheelEntity ;
186
+ break ;
187
+ case WheelPlacement . FrontLeft :
188
+ antiRoll . FrontLeftWheel = wheelEntity ;
189
+ break ;
190
+ case WheelPlacement . RearRight :
191
+ antiRoll . RearRightWheel = wheelEntity ;
192
+ break ;
193
+ case WheelPlacement . RearLeft :
194
+ antiRoll . RearLeftWheel = wheelEntity ;
195
+ break ;
196
+ }
197
+
198
+ EntityManager . AddComponentData ( wheelEntity , wheel ) ;
199
+ EntityManager . AddComponentData ( wheelEntity , suspension ) ;
200
+ EntityManager . AddComponentData ( wheelEntity , chassis ) ;
201
+ EntityManager . AddComponent < WheelDriveControls > ( wheelEntity ) ;
202
+ EntityManager . AddComponent < WheelHitData > ( wheelEntity ) ;
203
+ }
204
+
205
+ EntityManager . AddComponentData ( entity , new VolumeData ( )
206
+ {
207
+ Min = vehicleAuthoring . MinAudioVolume ,
208
+ Max = vehicleAuthoring . MaxAudioVolume ,
209
+ } ) ;
210
+ EntityManager . AddComponentData ( entity , antiRoll ) ;
211
+ EntityManager . AddComponentData ( entity , vehicleChassis ) ;
212
+ EntityManager . AddComponent < LapProgress > ( entity ) ;
213
+ EntityManager . AddComponent < Skin > ( entity ) ;
214
+ EntityManager . AddComponent < PlayerName > ( entity ) ;
215
+ EntityManager . AddComponent < Rank > ( entity ) ;
216
+ EntityManager . AddComponent < Reset > ( entity ) ;
217
+ } ) . Run ( ) ;
218
+ }
219
+
220
+ // Calculates the distance between front and rear wheels
221
+ private float GetWheelsBaseDistance ( VehicleAuthoring . WheelAuthoringHelper [ ] wheels )
222
+ {
223
+ var frontLeft = wheels . First ( w => w . Placement == WheelPlacement . FrontLeft ) ;
224
+ var rearLeft = wheels . First ( w => w . Placement == WheelPlacement . RearLeft ) ;
225
+ return math . distance ( frontLeft . WheelSpring . transform . position , rearLeft . WheelSpring . transform . position ) ;
226
+ }
227
+
228
+ // Calculates the distance between the rear wheels
229
+ private float GetRearTrackDistance ( VehicleAuthoring . WheelAuthoringHelper [ ] wheels )
230
+ {
231
+ var rearRight = wheels . First ( w => w . Placement == WheelPlacement . RearRight ) ;
232
+ var rearLeft = wheels . First ( w => w . Placement == WheelPlacement . RearLeft ) ;
233
+ return math . distance ( rearLeft . WheelSpring . transform . position , rearRight . WheelSpring . transform . position ) ;
234
+ }
235
+ }
236
+ }
0 commit comments