1
\$\begingroup\$

I'm trying to synchronize a rotating propeller with a engine sound by changing the sound's pitch.

I have a script attached to an airplane's propeller which, after the game starts, causes it to gradually increase rotation speed from slow to maximum.

 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 an empty gameobject I added an AudioSource and made its AudioClip an .mp3 sound effect of a propeller. To this gameobject I attached this 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 :

propeller

and a screenshot of the gameobject with the AudioSource component:

empty gameobject

The pitch in the audio source by default starts at value 1 and should be changed between the values of -3 and 3. The rotation speed in the Spin script is in other units.

The behavior I want is: as the propeller starts rotating slowly and increases to max speed, the audio source pitch will automatically change its value in sync with the propeller rotation speed.

However, the script AudioExample as it is now is making the pitch value to well exceed 3, in fact it goes up to 10. The calculation in the AudioExample script is wrong and I'm not sure how to correct it:

 _audioSource.pitch += Time.deltaTime * startingPitch / spin.RotationSpeed; 
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Seems like there are some strange things going on in the AudioExample code.

first of all, the pitch parameter in AudioSource is a float not an int. It would make sense to have the variables that affect pitch also be floats.

As a starting point, I would simply update the AudioSource.pitch at the same time I am updating the propeller rotation speed. You'll need to scale the pitch range to the rotation speed range as well if I understand what you are trying to do.

Your pitch range is -3 to 3 and your rotation speed is 0 to 10, so this math should do the trick:

p = s x (6/10) - 3

So, resulting code would look something like this:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Spin : MonoBehaviour { public float RotationSpeed = 1; [SerializeField] AudioSource propellerSoundSource; //populate in inspector // Start is called before the first frame update void Start() { propellerSoundSource.pitch = -3f; } // Update is called once per frame void Update() { transform.Rotate(0, 0, RotationSpeed, Space.Self); if (RotationSpeed < 10) { RotationSpeed += 1f * Time.deltaTime; propellerSoundSource.pitch = (RotationSpeed * .6f) -3f; } } } 
\$\endgroup\$
4
  • \$\begingroup\$ almost working but i found two problems or bug's. the first is that the pitch value is starting from -3 when i'm running the game and i want to start it when the pitch is at value 0. i tried to set the pitch value in the editor in the audio source component to 0 but it's starting from -3 when running the game. so the sound is like the engine get slowly turned off then when getting to 0 it's starting the engine on again and i want it to start from 0 like starting the engine only. \$\endgroup\$ Commented Sep 7, 2022 at 21:44
  • \$\begingroup\$ the second problem is that there is no sound at all and i found in google that if you don't enable the loop property in the audio source and changing the pitch there will be no sound. loop must be enabled. not sure if it's a bug but loop must be enabled and then when the pitch is reaching the max value with the rotation you can hear the looping of the sound like stopping for a millisecond or so and start over the sound again. \$\endgroup\$ Commented Sep 7, 2022 at 21:46
  • 1
    \$\begingroup\$ for the first issue, change the line in Start() that initializes the pitch from -3 to 0. \$\endgroup\$ Commented Sep 7, 2022 at 22:42
  • 1
    \$\begingroup\$ The second issue may be due to your AudioClip not matching perfectly. Not sure how to diagnose without the AudioClip to test. \$\endgroup\$ Commented Sep 7, 2022 at 22:45

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.