- Home /
Set audio.pitch limits
Hi to all! How can I do to set up in the c# script the audio.pitch limits so?
min-speed: 0.0 max-speed: 3.0
I tried to find the answer in old questions, but i don't found it. Thank you!
Answer by Magso · Feb 12, 2014 at 06:43 PM
You can make a variables like:
var soundPitch : float = 0;
var maxSpeed : float = 3;
var minSpeed : float = 0;
Then change the pitch variable by making the function and control, for example:
function Update(){
if(Input.GetKey("up")){
soundPicth++;
audio.pitch = (soundPitch);
//Brackets may not be needed on the variable here.
}
}
Then set the limits:
soundPitch = Mathf.Clamp(soundPitch, minSpeed, maxSpeed);
Thank you for your reply, I made so, but the increase/decrease is too fast:
using UnityEngine;
using System.Collections;
public class Audio : $$anonymous$$onoBehaviour
{
public AudioClip $$anonymous$$otore;
float sound$$anonymous$$ch = 0;
float maxSpeed = 3;
float $$anonymous$$Speed = 0;
void Start()
{
audio.clip = $$anonymous$$otore;
if (!audio.playOnAwake) audio.Play();
}
// Update is called once per frame
void Update ()
{
sound$$anonymous$$ch = $$anonymous$$athf.Clamp(sound$$anonymous$$ch, $$anonymous$$Speed, maxSpeed);
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Z))
{
sound$$anonymous$$ch++;
audio.pitch = (sound$$anonymous$$ch);
}
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.X))
{
sound$$anonymous$$ch--;
audio.pitch = (sound$$anonymous$$ch);
}
}
}
Try replacing the sound$$anonymous$$ch++(--) to sound$$anonymous$$ch+0.5.
Script editor says that i cannot do this.. is an error :/
C# requires the letter 'f' beside floats and should be assigning that calculation to the variable, writing just '1+1' for example wouldn't do anything
sound$$anonymous$$ch += 0.5f;
is the same, the error message is: only assignment, call, increment, decrement, and new object expressions can be used as a statement.