WheelDust.cs
1.88 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Valve.VR.InteractionSystem.Sample
{
public class WheelDust : MonoBehaviour
{
private WheelCollider col;
public ParticleSystem p;
public float EmissionMul;
public float velocityMul = 2;
public float maxEmission;
public float minSlip;
[HideInInspector]
public float amt;
[HideInInspector]
public Vector3 slip;
private float emitTimer;
private void Start()
{
col = GetComponent<WheelCollider>();
StartCoroutine(emitter());
}
private void Update()
{
slip = Vector3.zero;
if (col.isGrounded)
{
WheelHit hit;
col.GetGroundHit(out hit);
slip += Vector3.right * hit.sidewaysSlip;
slip += Vector3.forward * -hit.forwardSlip;
//print(slip);
}
amt = slip.magnitude;
//print(amt);
}
private IEnumerator emitter()
{
while (true)
{
while (emitTimer < 1)
{
yield return null;
if (amt > minSlip)
{
emitTimer += Mathf.Clamp((EmissionMul * amt), 0.01f, maxEmission);
}
}
emitTimer = 0;
DoEmit();
}
}
private void DoEmit()
{
p.transform.rotation = Quaternion.LookRotation(transform.TransformDirection(slip));
#if UNITY_2017_1_OR_NEWER
ParticleSystem.MainModule mainModule = p.main;
mainModule.startSpeed = velocityMul * amt;
#else
p.startSpeed = velocityMul * amt;
#endif
p.Emit(1);
}
}
}