Ajust Audio Pitch With Math.Clamp

Hello All,

Im A little stuck here but i hope someone with more experience can help me figure this out.
In return for a correct answer i will give a free copy of an my Crosshair asset that is on its way to the asset store. (RELEASED) Ultimate Crosshair Package - Community Showcases - Unity Discussions

I have wrote a script that i am trying to include audio with, basically once the update is called play the sound loop it and adjust the pitch with the existing math.clamp. I cant however figure out how to incorporate both.

Thankyou for anyone willing to help.

using UnityEngine;
using System.Collections;

public class SpinUpSpinDown : MonoBehaviour {


    private bool Fire;

    private float SpinUpTime = 1.5f; // spin up time
    private float SpinUpTimer;
    private float MaxSpinRate = 25f; // degrees per second
    private float audioClipSpeed = 10.0;

    public AudioClip SoundSpinning = null;
    protected AudioSource m_Audio = null;

    void Start()
    {
        m_Audio = GetComponent<AudioSource>();
        m_Audio.clip = SoundSpinning;
    }

    void Update()
    {

        Fire = Input.GetButton("Fire2");
        if (Fire)
        {  
            // Spin Increase
            SpinUpTimer = Mathf.Clamp (SpinUpTimer + Time.deltaTime,0, SpinUpTime);
          
            if (SpinUpTimer >= SpinUpTime)
            {
                //Do Somthing Here
            }
        } else {
          
            // Spin Decrease
            SpinUpTimer = Mathf.Clamp (SpinUpTimer - Time.deltaTime,0, SpinUpTime);
        }
      
        // Spin Transform
        float Barrel = (SpinUpTimer / SpinUpTime) * MaxSpinRate * Time.deltaTime;
        transform.RotateAroundLocal (Vector3.forward,Barrel);

    }
}

Would something like this work ?

m_Audio.pitch = 10f + (SpinUpTimer / SpinUpTime) * MaxSpinRate * Time.deltaTime;

I don’t know why i bother posting lol i always seem to find the answer before i get help and that usually doesn’t happen :frowning:
I guess i get my own asset.

Here it is for anybody looking how to do it.

using UnityEngine;
using System.Collections;

public class SpinUpSpinDown : MonoBehaviour {
  
    public float lowPitch = 0.5f;
    public float highPitch = 1f;

    private bool Fire;
    private float SpinUpTime = 1.5f; // spin up time
    private float SpinUpTimer;
    private float MaxSpinRate = 25f; // degrees per second
    private float audioClipSpeed = 10.0;
  
    public AudioClip SoundSpinning = null;
    protected AudioSource m_Audio = null;
  
    void Start()
    {
        m_Audio = GetComponent<AudioSource>();
        m_Audio.loop = true;
    }
  
    void Update()
    {
      
        Fire = Input.GetButton("Fire2");
        if (Fire)
        {
            // Spin Increase
            SpinUpTimer = Mathf.Clamp (SpinUpTimer + Time.deltaTime,0, SpinUpTime);
          
            if (SpinUpTimer >= SpinUpTime)
            {
                //Do Somthing Here
            }
        } else {
          
            // Spin Decrease
            SpinUpTimer = Mathf.Clamp (SpinUpTimer - Time.deltaTime,0, SpinUpTime);
        }
      
        // Spin Transform
        float Barrel = (SpinUpTimer / SpinUpTime) * MaxSpinRate * Time.deltaTime;
        transform.RotateAroundLocal (Vector3.forward,Barrel);

        m_Audio.pitch = Mathf.Lerp(lowPitch, highPitch, SpinUpTimer / SpinUpTime);
      
    }
}

I don’t think you want Time.deltaTime in there.
I don’t have any experience playing audio files in Unity but why don’t you try something like this:

var lowPitch = 0.5f;
var highPitch = 2f;
m_Audio.pitch = Mathf.Lerp(lowPitch, highPitch, SpinUpTimer / SpinUpTime);
1 Like

Hey elsenpony,

i just posted that and you replied lol. I wanted to be able to use the existing logic of the spin to calculate my pitch. And i ended up figuring it out.

And your probable right about that deltaTime in there.

Thankyou for the reply and for your kindness if you need some crosshairs let me know :slight_smile:

Mmmm that ramen looks good!!

n/p. I skipped your question earlier because I’m not familiar with audio. Once you had a sample code with what you were trying, it made things easier for me to guess.

I’m not sure your code makes any sense to me but as long as it works how you want!

Regarding getting help - there are about two dozen or so regulars who post quick answers but we are on at all different times of day. You can game the system a little bit by waiting to ask until there is a bunch of activity on the forum. Also, don’t be above bumping your question if someone who was helping seems to have dropped off. They might not be back for 24hrs or more! It’s also very helpful to see what you have attempted already in code.

I don’t need any crosshairs right now but I know where to go if that changes :wink:

I ended up doing it your way and lerping. It just made sense to be able to have a high and low var. Thankyou again for the incite in both aspects (Code/Forum).