WheelDust.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Valve.VR.InteractionSystem.Sample
  5. {
  6. public class WheelDust : MonoBehaviour
  7. {
  8. private WheelCollider col;
  9. public ParticleSystem p;
  10. public float EmissionMul;
  11. public float velocityMul = 2;
  12. public float maxEmission;
  13. public float minSlip;
  14. [HideInInspector]
  15. public float amt;
  16. [HideInInspector]
  17. public Vector3 slip;
  18. private float emitTimer;
  19. private void Start()
  20. {
  21. col = GetComponent<WheelCollider>();
  22. StartCoroutine(emitter());
  23. }
  24. private void Update()
  25. {
  26. slip = Vector3.zero;
  27. if (col.isGrounded)
  28. {
  29. WheelHit hit;
  30. col.GetGroundHit(out hit);
  31. slip += Vector3.right * hit.sidewaysSlip;
  32. slip += Vector3.forward * -hit.forwardSlip;
  33. //print(slip);
  34. }
  35. amt = slip.magnitude;
  36. //print(amt);
  37. }
  38. private IEnumerator emitter()
  39. {
  40. while (true)
  41. {
  42. while (emitTimer < 1)
  43. {
  44. yield return null;
  45. if (amt > minSlip)
  46. {
  47. emitTimer += Mathf.Clamp((EmissionMul * amt), 0.01f, maxEmission);
  48. }
  49. }
  50. emitTimer = 0;
  51. DoEmit();
  52. }
  53. }
  54. private void DoEmit()
  55. {
  56. p.transform.rotation = Quaternion.LookRotation(transform.TransformDirection(slip));
  57. #if UNITY_2017_1_OR_NEWER
  58. ParticleSystem.MainModule mainModule = p.main;
  59. mainModule.startSpeed = velocityMul * amt;
  60. #else
  61. p.startSpeed = velocityMul * amt;
  62. #endif
  63. p.Emit(1);
  64. }
  65. }
  66. }