I have some airplane with a propeller and a script attached to the propeller making the propeller start spinning when running the game from slow to max speed.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spin : MonoBehaviour { public float RotationSpeed = 1; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { transform.Rotate(0, 0, RotationSpeed, Space.Self); if (RotationSpeed < 10) { RotationSpeed += 1f * Time.deltaTime; } } } on empty gameobject i added audio source and dragged to it a mp3 sound effect of propeller and also attached to the empty gameobject a script and dragged to the script the propeller object with the Spin script :
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioExample : MonoBehaviour { public int startingPitch = 4; public int decreaseAmount = 5; public Spin spin; private AudioSource _audioSource; void Start() { _audioSource = GetComponent<AudioSource>(); _audioSource.pitch = startingPitch; } void Update() { if (_audioSource.pitch > 0) { _audioSource.pitch += Time.deltaTime * startingPitch / spin.RotationSpeed; } } } Screenshot of the propeller object :
and a screenshot of the empty gameobject with the audio source :
The pitch in the audio source by default start at value 1 and be changed between the values -3 and 3. The rotation speed in the Spin script is in other units.
what i want to do is when i'm running the game and the propeller start rotating slowly to max speed that the pitch automatic will change it's value according to the propeller rotation speed so the pitch sound and the propeller rotation will be sync.
The script AudioExample as it is now making the pitch value to be much over the 3 value over 10. The calculation in the AudioExample script is wrong and i'm not sure how to do it :
_audioSource.pitch += Time.deltaTime * startingPitch / spin.RotationSpeed; 
