• 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 Bibrosko · Sep 27, 2018 at 06:42 PM · soundaudiosourcerigidbody physics

play a sound when/while rigidbody velocity is greater than a value

hi guys, I wrote this simple script to play a sound when the enemy, attracted by the player, starts moving. however is not working, no sound is played (I attached an audio source with an audio clip to the gameobject). what did I do wrong?

also I'd like to implement another constant sound while the enemy is moving, after the initial clip is played.

any idea? thanks.

 using UnityEngine;
 using System.Collections;
 
 
 public class EnemySound : MonoBehaviour
 {
     AudioSource AudioSource;
     AudioClip hihihi;
     public Rigidbody enemy;
 
     void Start()
     {
         AudioSource.clip = hihihi;
         AudioSource.enabled = true;
         enemy = GetComponent<Rigidbody>();
 
     }
 
     void FixUpdate()
     {
         if (enemy.velocity.magnitude >= 0.1 && AudioSource.isPlaying == false)
         {
             AudioSource.PlayOneShot(hihihi, 0.7F);
         }
 
 
     }
 }
Comment
Add comment · Show 1
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 hexagonius · Sep 27, 2018 at 07:09 PM 0
Share

you must not call a variable the exact same as the type:

 AudioSource AudioSource;

secondly, it's FixedUpdate, not FixUpdate

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by abdullahsultan14 · Sep 27, 2018 at 07:26 PM

I believe it is FixedUpdate() rather than FixUpdate() And also change the variable name of AudioSource to something other than the reference name itself

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
avatar image
0

Answer by Bibrosko · Sep 27, 2018 at 10:44 PM

I corrected the code as suggested but the audio clip still does not work

Comment
Add comment · Show 6 · 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 abdullahsultan14 · Sep 28, 2018 at 02:50 AM 0
Share

Try adding debug logs in the if statement. Also add debug logs in the update method to keep track of the velocity and do post the updated code if possible

avatar image Bibrosko · Sep 28, 2018 at 01:21 PM 0
Share

I partially solved the issue doing this. Now the Enemy sound plays but it repeats in a loop ins$$anonymous$$d to play only one time. How can i make it stop after the first execution?

 using UnityEngine;
 using System.Collections;
 
 
 public class EnemySound : $$anonymous$$onoBehaviour
 {
     AudioSource EnemyAudioSource;
     public AudioClip hihihi;
     public Rigidbody enemy;
 
 
 
     void Start()
     {
         EnemyAudioSource = GetComponent<AudioSource>();
         EnemyAudioSource.enabled = true;
         enemy = GetComponent<Rigidbody>();
 
     }
 
     void FixedUpdate()
     {
         if (enemy.velocity.magnitude >= 1 && EnemyAudioSource.isPlaying == false)
         {
             EnemyAudioSource.PlayOneShot(hihihi, 0.7F);
 
 
         }
 
     }
 }
avatar image abdullahsultan14 Bibrosko · Sep 28, 2018 at 06:04 PM 0
Share

That is because of the FixedUpdate. As it is usually used to detect changes in a rigid body. And since the rigid body is moving it will keep on detecting this change. You will need to see how you are adjusting the magnitude of the rigid body. As long as the magnitude is greater than 1 the if statement will keep on executing

avatar image Bibrosko abdullahsultan14 · Sep 30, 2018 at 04:28 PM 0
Share

so there's no other way to play a sound "on moving"?

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

96 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 avatar image avatar image avatar image

Related Questions

What is your solution? (Zombie Sounds) 1 Answer

How to get spatial 3D sound on Video Player 1 Answer

Audio in WEBGL on IOS devices -1 Answers

Audio Play Time Inconsistent 0 Answers

Problem with Footstep System 1 Answer


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