Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by quantumarchi · May 18, 2012 at 11:04 AM · audioupdatetimeif-statementsclip

audio troubles changing from one to another (stop it consistently changing)

so i have this code and the clip keeps changing , basically i need it to finish playing the clip until the person stops moving forward and starts being idle at which point it needs to change to the idle sound effects without constantly changing the idle sound effect(just to be clear both idle sound effect clips and walking effect clips keep changing so i get literally a frames sound from the clip before it changes to another)

they also need to loop.

thanks for any help in advance ! using UnityEngine;

using System.Collections;

public class fxs : MonoBehaviour {

 public GameObject player;
 public bool reset = false;
 public AudioClip[] walk;
 public AudioClip[] idle;
 
 
 // Use this for initialization
 void Start () {
     player = GameObject.FindGameObjectWithTag("Player");
 }
 
 // Update is called once per frame
 void Update () {
     
     PickFx();
 }
 
 void PickFx() {
     TP_Animator other = player.GetComponent<TP_Animator>();
     if(other.walking == true){
         //problem is here 
             audio.clip = walk[Random.Range(0, walk.Length)];
             audio.Play();
                 
     
     }
     else if(other.walking == false)
     {
         audio.clip = idle[Random.Range(0, idle.Length)];
             audio.Play();
     }    
 }

}

it changes constantly because walking is constantly returning true so i keep getting it changing clip same with if false(idle sounds) im aware i put pickfx in update but i dont know where else to put it if there is one.

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
Best Answer

Answer by aldonaletto · May 18, 2012 at 11:48 AM

You could either wait for the current sound to end before playing a new one, or wait for some delay. Let's use the delay in the walking state, and the other alternative in the idle case:

float stepTime = 0; float stepInterval = 0.3f;

void PickFx() { TP_Animator other = player.GetComponent(); if(other.walking == true){ // if walking... if (Time.time >= stepTime){ // and stepTime has come... audio.clip = walk[Random.Range(0, walk.Length)]; audio.Play(); // play the walk sound... stepTime = Time.time + stepInterval; // and set new stepTime } } else // if not walking... { if (audio.isPlaying == false){ // and previous sound has finished... audio.clip = idle[Random.Range(0, idle.Length)]; audio.Play(); // play random idle sound } } } The interval approach is better for footstep sounds - you can even change stepInterval according to the character speed - while the other alternative is better for generating a continuous random sequence of sounds.

Comment
Add comment · Show 2 · 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
avatar image quantumarchi · May 18, 2012 at 11:59 AM 0
Share

works great thank you . i think i might need to do some work on my step sound to make it fit better but it works! thanks

avatar image aldonaletto · May 18, 2012 at 12:08 PM 0
Share

You can use isPlaying in both cases, depending on how your walk sounds are (a worm crawling, for instance) - but for footsteps the delay approach is the best choice.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

New Input system holding down a button 2 Answers

Audio Script 1 Answer

Slow motion all but one - yet another round, hopefully the last 6 Answers

Making sounds wait for each other to finish 2 Answers

Slow update best practice 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