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 hollym16 · Sep 27, 2013 at 10:43 AM · animationgameobjectaudioaudiosource

GameObject reacts to audio source

I was wondering if there was a way to link a GameObject to an audio source to make it react with it? Like you can do in AfterEffects.

Currently, I just want the GameObject to move up and down in time with the beat of the music so nothing too ambitious. Anyone know if this can be done or how to go about it?

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 TimXT · Sep 29, 2013 at 12:29 PM

Wow, cool question!

Hmmm, there is one way which is using AudioClip.GetData(); This gives you access to the samples of an audio clip which I think you can use to check what the actual volume level of the audio source is (not the overall volume, but the actual level at that instant).

You can try doing this:

On the game object of the audio source attach this script:

 float[] samples;
     
     void Start()
     {
       //make an array to contain all the samples of the clip currently playing
        samples = new float[audio.clip.samples*audio.clip.channels];
 
       //store all the sample data of the clip in this array.
        audio.clip.GetData(samples, 0);
 
     }
     
     void Update()
     {
        //in every frame get the current sample of the clip from the samples array.
        float currentSample = samples[audio.timeSamples];
     
        /*then do whatever you want with it,
         e.g. you can use it to set the Y position
         of a gameobject so that it moves up and down
         according to the beat. */
     }

You can also check the current sample level at longer intervals using coroutines instead of on update, to avoid the game object from jerking too much on little volume changes.

A downside of this though is that the audio clip you are playing cannot be compressed or streaming which will increase the size of the sound file.

Anyways, hope this helps!

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 BoldBigflank · Dec 13, 2013 at 07:59 AM 0
Share

I used this code, and it's really close. Assigning the samples wasn't accounting for multiple channels. Line 16 needs to look like: float currentSample = samples[audio.timeSamples*audio.clip.channels]; That will get the first channel of each sample.

avatar image fafase · Dec 13, 2013 at 08:10 AM 0
Share

You could add this to the frequency. Often the snare has a high frequency when the bass drum has a low frequency. This way you could make the object move in accordance to the frequency of the wave.

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

17 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

Related Questions

Music works but not when accessed by another script in JavaScript 1 Answer

What is the script code on how to trigger a song for the player to Listen to? 2 Answers

Playing a sound clip from another object 1 Answer

Slender like game 3 Answers

Audio source in two different scenes 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