It’s like making the propeller with the pitch sound engine starting effect.
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;
}
}
}
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 :
If I understand correctly, you want to map your current prop rotation speed to the -3 to 3 sound pitch values.
I’m no maths expert, but how about this for a starting point:
In Update(),
Normalize the current rotation speed, i.e. squash values to between 0 - 1 normalizedValue = (currentValue - minValue) / (maxValue - minValue)
You’d need your max rotation speed for maxValue here, presumable min rotation speed is zero, and your currentValue is just your current rotation speed.
Multiply it by 6 to get range 0 - 6
Subtract 3 to get range -3 to 3
Apply this value to your pitch.
If the lower end of the pitch sounds strange, you could also consider some form of volume control on the top. Not sure how they usually do this in flight sims.
but for some reason the pitch start from -2 even if i set it in the editor by default to 0.
second i need to uncheck the audio source and check it back to hear the sound. like yo disable and enabled back the audio source component while the game is running if not there is no sound even if the pitch is above 0.
Try making your ‘normalizedValue’ variable a public field so you can see its values in the inspector as it changes. Check to see it’s going from -3 to 3 first.
If it is correct, something is going on with the audio source. Instead of setting pitch in the editor, try setting pitch = 0 by default in the Start() function. At least you can be sure where it starts then.
i found in google that if i start the game and in the audio source component the loop property is enabled false then there will be no sound. either i should start the game when the audio source loop property is true or that the pitch will not be in negative value.
i guess it’s a bug or something in unity.
so far i couldn’t make the pitch start from 0.
i made the variable normalizedValue public and set the pitch to 0 in the Start() and still when running the game the pitch is starting from -2.975 (-3)
i also used a break point on the line in the Update
In what situation do you want your pitch to be zero? Right now, when your rotation is zero, your pitch will be -3, when rotation is max it’ll be 3, when rotation is half of max, pitch will be 0.
It sounds like you want a different behaviour, but I’m not sure what.
Maybe pitch 0 when rot speed is zero
Pitch 3 when rot speed is max
Seems sensible for a plane, but I’m not sure where the pitch -3 comes in to it
Why do you have (RotationSpeed - 0) and (10 - 0)? Subtracting zero doesn’t change the value. If RotationSpeed is 0, then normalizedValue will be -3. To find out for sure what is going on, use Debug.Log for all your variable runtime values. Once you do so, share your updated code with the Debug.Log statements and the Console output https://discussions.unity.com/t/748729/14
This is working but still there is the loop issue :
I moved everything to one script to the Spin script :
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 = 0f;
}
// 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 * .3f);
}
}
}
but still i need to start the game when the loop in the audio source is checked enabled if not when the pitch is reaching 3 and the propeller to max speed the sound stop. untill that there is sound but when it’s reaching to the max the pitch and the rotation the sound stop.
only if the loop is enabled true the sound will continue, but i don’t the sound to be loop or i want the sound to continue playing but if the loop is enabled true it sound like the sound stop each some seconds and then start again.