Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Audio;
- using UnityEngine.Experimental.VFX;
- public class FX_Speed : MonoBehaviour
- {
- public GameObject cPlayer;
- [SerializeField]
- public VisualEffect VFXGraph;
- [SerializeField, Range(0, 6000)]
- public float normalSpeed = 0;
- [SerializeField, Range(0, 6000)]
- public float warpSpeed = 0;
- public AudioMixerGroup output;
- public AudioClip engineSound;
- private float minPitch = 0.5f;
- private float maxPitch = 2f;
- // Start is called before the first frame update
- void Start()
- {
- VFXGraph.SetFloat("Speed2", normalSpeed);
- cPlayer = GameObject.FindGameObjectWithTag("Player");
- }
- // Update is called once per frame
- void Update()
- {
- PlayerController pC = cPlayer.GetComponentInChildren<PlayerController>();
- normalSpeed = pC.Speed*100;
- ////adjust pitch of engine sound
- float ePitch = pC.Speed / 10*maxPitch;
- float maxSpeed = pC.MaxSpeed;
- float warpMaxSpeed = pC.WarpSpeed;
- changePitch(ePitch);
- if (normalSpeed >= 6000)
- {
- normalSpeed = 6000;
- }
- if(normalSpeed > 0 && gameObject.GetComponent<AudioSource>() == null)
- {
- engineOn(ePitch);
- }
- if (normalSpeed <= 0)
- {
- normalSpeed = 0;
- engineOff();
- }
- VFXGraph.SetFloat("Speed2", normalSpeed);
- }
- public void changePitch(float enginePitch)
- {
- if (gameObject.GetComponent<AudioSource>() != null)
- {
- AudioSource source = gameObject.GetComponent<AudioSource>();
- if (enginePitch <= 0.5f)
- {
- enginePitch = 0.5f;
- }
- if (enginePitch >= 2f)
- {
- enginePitch = 2f;
- }
- source.pitch = enginePitch;
- Debug.Log("Current Pitch: " + enginePitch.ToString());
- }
- }
- public void engineOn(float enginePitch)
- {
- AudioSource source = gameObject.AddComponent<AudioSource>();
- source.clip = engineSound;
- source.volume = 1;
- source.spatialBlend = 1f;
- source.minDistance = 0.1f;
- source.maxDistance = 5f;
- source.loop = true;
- source.pitch = enginePitch;
- source.Play();
- }
- public void engineOff()
- {
- AudioSource source = gameObject.GetComponent<AudioSource>();
- Destroy(source);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement