Skip to content

Commit 8972ef5

Browse files
committed
Now the wheel can rotate when accelerating
1 parent 6875c23 commit 8972ef5

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

Assets/Source/Veichle/VeichleSimulation.cs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,22 @@ public class VeichleSimulation : MonoBehaviour, IVeichleControls
1111
public float maxTurnAngle;
1212
public float turnSpeed;
1313

14-
public GameObject wheelModelsGo;
1514
public GameObject wheelColliderGo;
1615

1716
IVeichleControls controller;
1817

1918
WheelCollider[] wheelsColliders;
20-
Transform[] anteriorWheels;
19+
public Transform[] wheelsTransforms;
20+
public Transform[] anteriorWheels;
2121

2222
float accel;
2323
float angle;
24+
float lastDirection;
2425

25-
// Use this for initialization
26-
void Start ()
26+
// Use this for initialization
27+
void Start()
2728
{
2829
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);
3430
}
3531

3632
public void Accelerate(float accelValue)
@@ -39,7 +35,7 @@ public void Accelerate(float accelValue)
3935
{
4036
accel = -accelValue * accelSpeed;
4137
}
42-
else if(accelValue < 0)
38+
else if (accelValue < 0)
4339
{
4440
accel = -accelValue * accelSpeed * 0.5f;
4541
}
@@ -57,9 +53,9 @@ public void Accelerate(float accelValue)
5753

5854
public void Steer(float steerValue)
5955
{
60-
if(steerValue == 0)
56+
if (steerValue == 0)
6157
{
62-
if(angle> 0)
58+
if (angle > 0)
6359
{
6460
angle -= Time.deltaTime * maxTurnAngle * turnSpeed;
6561
angle = Mathf.Max(angle, 0);
@@ -77,10 +73,9 @@ public void Steer(float steerValue)
7773
angle = Mathf.Clamp(angle, -maxTurnAngle, maxTurnAngle);
7874
}
7975

80-
for (int i = 0; i<2; i++)
76+
for (int i = 0; i < 2; i++)
8177
{
8278
wheelsColliders[i].steerAngle = angle;
83-
anteriorWheels[i].localRotation = Quaternion.Euler(0, angle, 0);
8479
}
8580

8681
}
@@ -100,6 +95,40 @@ public void Brake(bool isBraking)
10095
}
10196
}
10297

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+
}
103132

104133
// Usefull to communicate events to the Controller
105134
public void Notify(string message)

0 commit comments

Comments
 (0)