The code below is being used in a Siren Simulator Software that I am creating. Basically what I cant figure out is how to change the ending pitch cut off from being the same I.E The pitch of the audio be lower than that of the starting pitch. Its basically when you hold down the button, the audio winds up and stays at a certain limit and when you let go it winds down slower than the wind up and ends at the same pitch level. What I want it to do is be able to end at a lower pitch than the starting pitch. Any edits to the scripts or ideas would help ALOT!
using UnityEngine;
using System.Collections;
public class PitchChange : MonoBehaviour
{
public AudioSource Audio;
public float StartingPitch = 1.0f;
public float EndingPitch = 2.0f;
public float PitchUpScaleFactor = 3.0f;
public float PitchDownScaleFactor = 2.0f;
bool isMousePressed = false;
void OnMouseDown()
{
isMousePressed=true;
}
void OnMouseUp()
{
isMousePressed=false;
}
// Update is called once per frame
void Update ()
{
//If mouse is pressed, increase pitch
if(isMousePressed == true)
{
if(Audio.pitch <= StartingPitch)
{
Audio.Play();
}
//Only increase pitch if "EndingPitch" value has not been reached
if(Audio.pitch <= EndingPitch)
{
Audio.pitch += ((Time.deltaTime * StartingPitch) / PitchUpScaleFactor);
}
}
//Otherwise, decrease pitch back to normal, until we reach starting pitch
else
{
if(Audio.pitch > StartingPitch)
{
Audio.pitch -= ((Time.deltaTime * StartingPitch) / PitchDownScaleFactor);
}
else
{
Audio.Stop();
}
}
Debug.Log(Audio.pitch);
}
}
well I still want it to be able to go higher than the starting pitch I.E starting pitch 3 max pitch 6 ending pitch 1 Sorry Im asking alot!
using UnityEngine;
using System.Collections;
public class PitchChange : MonoBehaviour
{
public AudioSource Audio;
public float StartingPitch = 1.5f;
public float EndingPitch = 1.0f;
public float MaxPitch = 2.5f;
public float PitchUpScaleFactor = 3.0f;
public float PitchDownScaleFactor = 2.0f;
bool isMousePressed = false;
void OnMouseDown()
{
isMousePressed=true;
}
void OnMouseUp()
{
isMousePressed=false;
}
// Update is called once per frame
void Update ()
{
//If mouse is pressed, increase pitch
if(isMousePressed == true)
{
if(Audio.isPlaying == false)
{
Audio.pitch = StartingPitch;
Audio.Play();
}
//Only increase pitch if "EndingPitch" value has not been reached
if(Audio.pitch <= MaxPitch)
{
Audio.pitch += ((Time.deltaTime * StartingPitch) / PitchUpScaleFactor);
}
}
//Otherwise, decrease pitch back to normal, until we reach starting pitch
else
{
if(Audio.pitch > EndingPitch)
{
Audio.pitch -= ((Time.deltaTime * StartingPitch) / PitchDownScaleFactor);
}
else
{
Audio.Stop();
}
}
}
}