@@ -11,26 +11,22 @@ public class VeichleSimulation : MonoBehaviour, IVeichleControls
11
11
public float maxTurnAngle ;
12
12
public float turnSpeed ;
13
13
14
- public GameObject wheelModelsGo ;
15
14
public GameObject wheelColliderGo ;
16
15
17
16
IVeichleControls controller ;
18
17
19
18
WheelCollider [ ] wheelsColliders ;
20
- Transform [ ] anteriorWheels ;
19
+ public Transform [ ] wheelsTransforms ;
20
+ public Transform [ ] anteriorWheels ;
21
21
22
22
float accel ;
23
23
float angle ;
24
+ float lastDirection ;
24
25
25
- // Use this for initialization
26
- void Start ( )
26
+ // Use this for initialization
27
+ void Start ( )
27
28
{
28
29
wheelsColliders = wheelColliderGo . GetComponentsInChildren < WheelCollider > ( ) ;
29
-
30
- anteriorWheels = new Transform [ 2 ] ;
31
-
32
- anteriorWheels [ 0 ] = wheelModelsGo . transform . GetChild ( 0 ) ;
33
- anteriorWheels [ 1 ] = wheelModelsGo . transform . GetChild ( 1 ) ;
34
30
}
35
31
36
32
public void Accelerate ( float accelValue )
@@ -39,7 +35,7 @@ public void Accelerate(float accelValue)
39
35
{
40
36
accel = - accelValue * accelSpeed ;
41
37
}
42
- else if ( accelValue < 0 )
38
+ else if ( accelValue < 0 )
43
39
{
44
40
accel = - accelValue * accelSpeed * 0.5f ;
45
41
}
@@ -57,9 +53,9 @@ public void Accelerate(float accelValue)
57
53
58
54
public void Steer ( float steerValue )
59
55
{
60
- if ( steerValue == 0 )
56
+ if ( steerValue == 0 )
61
57
{
62
- if ( angle > 0 )
58
+ if ( angle > 0 )
63
59
{
64
60
angle -= Time . deltaTime * maxTurnAngle * turnSpeed ;
65
61
angle = Mathf . Max ( angle , 0 ) ;
@@ -77,10 +73,9 @@ public void Steer(float steerValue)
77
73
angle = Mathf . Clamp ( angle , - maxTurnAngle , maxTurnAngle ) ;
78
74
}
79
75
80
- for ( int i = 0 ; i < 2 ; i ++ )
76
+ for ( int i = 0 ; i < 2 ; i ++ )
81
77
{
82
78
wheelsColliders [ i ] . steerAngle = angle ;
83
- anteriorWheels [ i ] . localRotation = Quaternion . Euler ( 0 , angle , 0 ) ;
84
79
}
85
80
86
81
}
@@ -100,6 +95,40 @@ public void Brake(bool isBraking)
100
95
}
101
96
}
102
97
98
+ void Update ( )
99
+ {
100
+ // Update visual of the models
101
+
102
+ // Steer of the wheels
103
+ foreach ( Transform wheel in anteriorWheels )
104
+ {
105
+ wheel . localRotation = Quaternion . Euler ( 0 , angle , 0 ) ;
106
+ }
107
+
108
+ float carVel = Vector3 . Magnitude ( GetComponentInParent < Rigidbody > ( ) . velocity ) ;
109
+
110
+ if ( accel != 0 )
111
+ {
112
+ lastDirection = Mathf . Sign ( - accel ) ;
113
+ }
114
+
115
+ float wheelRotationY = 0 ;
116
+
117
+ if ( Mathf . Abs ( carVel ) > 0.005 )
118
+ {
119
+ wheelRotationY = carVel * lastDirection ;
120
+ }
121
+ else
122
+ {
123
+ wheelRotationY = accel ;
124
+ }
125
+
126
+ // Rotate the wheels
127
+ foreach ( Transform wheel in wheelsTransforms )
128
+ {
129
+ wheel . localRotation *= Quaternion . Euler ( wheelRotationY , 0 , 0 ) ;
130
+ }
131
+ }
103
132
104
133
// Usefull to communicate events to the Controller
105
134
public void Notify ( string message )
0 commit comments