Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Audio;
  5. using UnityEngine.Experimental.VFX;
  6.  
  7. public class FX_Speed : MonoBehaviour
  8. {
  9. public GameObject cPlayer;
  10.  
  11. [SerializeField]
  12. public VisualEffect VFXGraph;
  13.  
  14. [SerializeField, Range(0, 6000)]
  15. public float normalSpeed = 0;
  16.  
  17. [SerializeField, Range(0, 6000)]
  18. public float warpSpeed = 0;
  19.  
  20. public AudioMixerGroup output;
  21. public AudioClip engineSound;
  22. private float minPitch = 0.5f;
  23. private float maxPitch = 2f;
  24.  
  25. // Start is called before the first frame update
  26. void Start()
  27. {
  28. VFXGraph.SetFloat("Speed2", normalSpeed);
  29. cPlayer = GameObject.FindGameObjectWithTag("Player");
  30. }
  31.  
  32. // Update is called once per frame
  33. void Update()
  34. {
  35. PlayerController pC = cPlayer.GetComponentInChildren<PlayerController>();
  36.  
  37. normalSpeed = pC.Speed*100;
  38.  
  39. ////adjust pitch of engine sound
  40. float ePitch = pC.Speed / 10*maxPitch;
  41. float maxSpeed = pC.MaxSpeed;
  42. float warpMaxSpeed = pC.WarpSpeed;
  43.  
  44. changePitch(ePitch);
  45.  
  46. if (normalSpeed >= 6000)
  47. {
  48. normalSpeed = 6000;
  49. }
  50. if(normalSpeed > 0 && gameObject.GetComponent<AudioSource>() == null)
  51. {
  52. engineOn(ePitch);
  53. }
  54.  
  55. if (normalSpeed <= 0)
  56. {
  57. normalSpeed = 0;
  58. engineOff();
  59. }
  60.  
  61. VFXGraph.SetFloat("Speed2", normalSpeed);
  62.  
  63. }
  64.  
  65. public void changePitch(float enginePitch)
  66. {
  67. if (gameObject.GetComponent<AudioSource>() != null)
  68. {
  69. AudioSource source = gameObject.GetComponent<AudioSource>();
  70.  
  71. if (enginePitch <= 0.5f)
  72. {
  73. enginePitch = 0.5f;
  74. }
  75.  
  76. if (enginePitch >= 2f)
  77. {
  78. enginePitch = 2f;
  79. }
  80. source.pitch = enginePitch;
  81. Debug.Log("Current Pitch: " + enginePitch.ToString());
  82. }
  83. }
  84.  
  85. public void engineOn(float enginePitch)
  86. {
  87. AudioSource source = gameObject.AddComponent<AudioSource>();
  88. source.clip = engineSound;
  89. source.volume = 1;
  90. source.spatialBlend = 1f;
  91. source.minDistance = 0.1f;
  92. source.maxDistance = 5f;
  93. source.loop = true;
  94. source.pitch = enginePitch;
  95. source.Play();
  96. }
  97.  
  98. public void engineOff()
  99. {
  100. AudioSource source = gameObject.GetComponent<AudioSource>();
  101. Destroy(source);
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement