BuggyBuddy.cs
7.44 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using UnityEngine;
using System;
using System.Collections;
namespace Valve.VR.InteractionSystem.Sample
{
public class BuggyBuddy : MonoBehaviour
{
public Transform turret;
float turretRot;
[Tooltip("Maximum steering angle of the wheels")]
public float maxAngle = 30f;
[Tooltip("Maximum Turning torque")]
public float maxTurnTorque = 30f;
[Tooltip("Maximum torque applied to the driving wheels")]
public float maxTorque = 300f;
[Tooltip("Maximum brake torque applied to the driving wheels")]
public float brakeTorque = 30000f;
[Tooltip("If you need the visual wheels to be attached automatically, drag the wheel shape here.")]
public GameObject[] wheelRenders;
[Tooltip("The vehicle's speed when the physics engine can use different amount of sub-steps (in m/s).")]
public float criticalSpeed = 5f;
[Tooltip("Simulation sub-steps when the speed is above critical.")]
public int stepsBelow = 5;
[Tooltip("Simulation sub-steps when the speed is below critical.")]
public int stepsAbove = 1;
private WheelCollider[] m_Wheels;
public AudioSource au_motor;
[HideInInspector]
public float mvol;
public AudioSource au_skid;
float svol;
public WheelDust skidsample;
float skidSpeed = 3;
public Vector3 localGravity;
[HideInInspector]
public Rigidbody body;
public float rapidfireTime = 0;
private float shootTimer;
[HideInInspector]
public Vector2 steer;
[HideInInspector]
public float throttle;
[HideInInspector]
public float handBrake;
[HideInInspector]
public Transform controllerReference;
[HideInInspector]
public float speed;
public Transform centerOfMass;
private void Start()
{
body = GetComponent<Rigidbody>();
m_Wheels = GetComponentsInChildren<WheelCollider>();
body.centerOfMass = body.transform.InverseTransformPoint(centerOfMass.position) * body.transform.lossyScale.x;
}
/*
private void TurretInput()
{
Vector2 tIn = TurretControl.joystick();
Vector3 tur = new Vector3(tIn.x, 0, tIn.y);
tur = TurretControl.transform.TransformDirection(tur);
tur = transform.InverseTransformDirection(tur);
tur = Vector3.ProjectOnPlane(tur, Vector3.up);
turretRot = VectorMath.FindAngle(Vector3.forward, tur, Vector3.up) * Mathf.Rad2Deg;
Vector3 turup = Vector3.forward;
turret.localRotation = Quaternion.Euler(turup * turretRot);
if (rapidfireTime == 0)
{
if (TurretControl.GetPressDown(KnucklesButton.Trigger))
{
Fire();
}
}else
{
if (shootTimer > rapidfireTime&& TurretControl.GetPress(KnucklesButton.Trigger))
{
Fire();
shootTimer = 0;
}
shootTimer += Time.deltaTime;
}
}
*/
private void Update()
{
m_Wheels[0].ConfigureVehicleSubsteps(criticalSpeed, stepsBelow, stepsAbove);
//TurretInput();
//keyboard input for testing
//Vector3 move = Vector3.forward * Input.GetAxis("Vertical") + Vector3.right * Input.GetAxis("Horizontal");
//driving input
//float forward = maxTorque * move.magnitude;
float forward = maxTorque * throttle;
if (steer.y < -0.5f)
forward *= -1;
float angle = maxAngle * steer.x;
speed = transform.InverseTransformVector(body.velocity).z;
float forw = Mathf.Abs(speed);
angle /= 1 + forw / 20;
// if (Mathf.Abs(move.z) < 0.1f && Mathf.Abs(move.x) > 0.5)
// forward *= 3;
//float forward = maxTorque * throttle; not fun lawrence steering
float fVol = Mathf.Abs(forward);
mvol = Mathf.Lerp(mvol, Mathf.Pow((fVol / maxTorque), 0.8f) * Mathf.Lerp(0.4f, 1.0f, (Mathf.Abs(m_Wheels[2].rpm) / 200)) * Mathf.Lerp(1.0f, 0.5f, handBrake), Time.deltaTime * 9);
au_motor.volume = Mathf.Clamp01(mvol);
float motorPitch = Mathf.Lerp(0.8f, 1.0f, mvol);
au_motor.pitch = Mathf.Clamp01(motorPitch);
svol = Mathf.Lerp(svol, skidsample.amt / skidSpeed, Time.deltaTime * 9);
au_skid.volume = Mathf.Clamp01(svol);
float skidPitch = Mathf.Lerp(0.9f, 1.0f, svol);
au_skid.pitch = Mathf.Clamp01(skidPitch);
//float forward = maxTorque * Input.GetAxis("Vertical");
//bool stopped = Mathf.Abs(transform.InverseTransformDirection(GetComponent<Rigidbody>().velocity).z) < 1.0f;
for (int i = 0; i < wheelRenders.Length; i++)
{
WheelCollider wheel = m_Wheels[i];
if (wheel.transform.localPosition.z > 0)
{
// front wheels
wheel.steerAngle = angle;
//4wd?
wheel.motorTorque = forward;
}
if (wheel.transform.localPosition.z < 0) // back wheels
{
}
// wheel.brakeTorque = Mathf.Lerp(Mathf.Abs(forward) < 0.1f ? 1 : 0, brakeTorque, handBrake);
wheel.motorTorque = forward;
if (wheel.transform.localPosition.x < 0) // left wheels
{
}
if (wheel.transform.localPosition.x >= 0) // right wheels
{
}
// Update visual wheels if they exist, and the colliders are enabled
if (wheelRenders[i] != null && m_Wheels[0].enabled)
{
Quaternion q;
Vector3 p;
wheel.GetWorldPose(out p, out q);
Transform shapeTransform = wheelRenders[i].transform;
shapeTransform.position = p;
shapeTransform.rotation = q;
}
}
steer = Vector2.Lerp(steer, Vector2.zero, Time.deltaTime * 4);
}
private void FixedUpdate()
{
body.AddForce(localGravity, ForceMode.Acceleration);
}
public static float FindAngle(Vector3 fromVector, Vector3 toVector, Vector3 upVector)
{
// If the vector the angle is being calculated to is 0...
if (toVector == Vector3.zero)
// ... the angle between them is 0.
return 0f;
// Create a float to store the angle between the facing of the enemy and the direction it's travelling.
float angle = Vector3.Angle(fromVector, toVector);
// Find the cross product of the two vectors (this will point up if the velocity is to the right of forward).
Vector3 normal = Vector3.Cross(fromVector, toVector);
// The dot product of the normal with the upVector will be positive if they point in the same direction.
angle *= Mathf.Sign(Vector3.Dot(normal, upVector));
// We need to convert the angle we've found from degrees to radians.
angle *= Mathf.Deg2Rad;
return angle;
}
}
}