- Home /
How can I call an audio clip in the update without it distorting?,How can i play an audio clip in the Update function?
Hello,
I am fairly new to coding and have been working on a little rolling ball game for a few months now and am stuck on adding a rolling sound to the ball when it moves. I have all my audio files in an audio manager script and call them when they need to be ran. I have a script that deals with the aspects of the balls movement that i would like to call the rolling clip. I have a boost function for the ball that the sound file I have works fine in the function. I just seem to have no idea how to incorporate the rolling sound when the ball moves. I keep going back to the update. Any help would be fantastic!
here is the script for the ball.
public class Motor : MonoBehaviour
{
private const float TIME_BEFORE_START = 3.0f;
public float moveSpeed = 5.0f;
public float drag = 0.5f;
public float terminalRotationSpeed = 25.0f;
public VirtualJoystick moveJoystick;
public float boostSpeed = 5.0f;
public float boostCooldown = 2.0f;
private float lastBoost;
private Rigidbody controller;
private Transform camTransform;
private float startTime = 0.0f;
public bool isRolling;
private void Start()
{
lastBoost = Time.time - boostCooldown;
startTime = Time.time;
controller = GetComponent<Rigidbody> ();
controller.maxAngularVelocity = terminalRotationSpeed;
controller.drag = drag;
camTransform = Camera.main.transform;
isRolling = false;
}
private void Update()
{
if (Time.time - startTime < TIME_BEFORE_START)
return;
Vector3 dir = Vector3.zero;
dir.x = Input.GetAxis ("Horizontal");
dir.z = Input.GetAxis ("Vertical");
if (dir.magnitude > 1)
{
dir.Normalize ();
}
if (moveJoystick.InputDirection != Vector3.zero)
{
dir = moveJoystick.InputDirection;
}
// Rotate our direction vector with camera
Vector3 rotatedDir = camTransform.TransformDirection (dir);
rotatedDir = new Vector3 (rotatedDir.x, 0, rotatedDir.z);
rotatedDir = rotatedDir.normalized * dir.magnitude;
controller.AddForce (rotatedDir * moveSpeed);
// code I have to add rolling sound if the ball is moving
if (moveSpeed >= 1f)
{
if (!isRolling) {
FindObjectOfType<AudioManager> ().Play ("Rolling_1");
isRolling = true;
} else
isRolling = false;
FindObjectOfType<AudioManager> ().Stop ("Rolling_1");
}
}
public void Boost()
{
if (Time.time - startTime < TIME_BEFORE_START)
return;
if (Time.time - lastBoost > boostCooldown)
{
lastBoost = Time.time;
controller.AddForce (controller.velocity.normalized * boostSpeed, ForceMode.VelocityChange);
FindObjectOfType<AudioManager> ().Play ("Boost");
}
}
}
Answer by danielburnitt · Mar 21, 2018 at 01:07 AM
I fixed it!
All I had to do was set my audio source i wanted to use for my rolling sound to loop at volume at 0 when the game starts. then in the update I edited the volume to when the ball speeds up the volume rises and then when the ball slows down the volume falls until the ball isnt moving and its at 0.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154830im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
What are the advantages of stereo sound effects? 0 Answers
How should I make this Audio play? 1 Answer
Is there a way to create a random Audiosource loop? 2 Answers
Sound creation using WAV. files 1 Answer
Problem with enabling and playing audios 0 Answers