Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by danielburnitt · Mar 18, 2018 at 07:41 AM · audioaudioclipupdate functionsound effects

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");
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges