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 TheShadyColombian · Oct 24, 2015 at 04:34 AM · c#visualizationvisualize

make visualizer smoother

I need some help making the visualizer on my game smoother. I have an example of what it's supposed to look like: https://www.youtube.com/watch?v=gRr9E9cVqHE

I tested this song in my game's visualizer, and it looks terrible and jittery (it's the jagged pink line in the background). I'm thinking I need to make the visualizer value to be interpolated from 5 frames or so. is there a quick way or doing that, or a better way to fix it?

Here's my current code (not originally mine):

 using UnityEngine;  
 using System.Collections;  
 
 public class AudioVisualizer : MonoBehaviour  
 {  
     
     //The velocity that the cubes will drop  
     public Vector3 gravity = new Vector3(0.0f,0.25f,0.0f);  
     //An AudioSource object so the music can be played  
     private AudioSource aSource;  
     //A float array that stores the audio samples  
     public float[] samples = new float[64];  
     //A renderer that will draw a line at the screen  
     private LineRenderer lRenderer;  
     //A reference to the cube prefab  
     public GameObject cube;  
     //The transform attached to this game object  
     private Transform goTransform;  
     //The position of the current cube. Will also be the position of each point of the line.  
     private Vector3 cubePos;  
     //An array that stores the Transforms of all instantiated cubes  
     private Transform[] cubesTransform;  
 
     void Awake ()  
     {  
         //Get and store a reference to the following attached components:  
         //AudioSource  
         this.aSource = GetComponent<AudioSource>();  
         //LineRenderer  
         this.lRenderer = GetComponent<LineRenderer>();  
         //Transform  
         this.goTransform = GetComponent<Transform>();  
     }  
 
     void Start()  
     {  
         //The line should have the same number of points as the number of samples  
         lRenderer.SetVertexCount(samples.Length);  
         //The cubesTransform array should be initialized with the same length as the samples array  
         cubesTransform = new Transform[samples.Length];  
 
 
         //Center the audio visualization line at the X axis, according to the samples array length  
         //goTransform.position = new Vector3 ((-samples.Length / 2) * cube.transform.localScale.x, goTransform.position.y, goTransform.position.z);  
 
         //Create a temporary GameObject, that will serve as a reference to the most recent cloned cube  
         GameObject tempCube;  
 
         //For each sample  
         for(int i=0; i<samples.Length;i++)  
         {  
             //Instantiate a cube placing it at the right side of the previous one  
             tempCube = (GameObject) Instantiate(cube, new Vector3(goTransform.position.x + i, goTransform.position.y, goTransform.position.z),Quaternion.identity);  
             //Get the recently instantiated cube Transform component  
             cubesTransform[i] = tempCube.GetComponent<Transform>();  
             //Make the cube a child of this game object  
             cubesTransform[i].parent = goTransform;  
         }  
     }  
 
     void Update ()  
     {  
 
         if (PlayerPrefs.GetInt ("UseMusic") == 1) {
 
             if (PlayerPrefs.GetInt ("UseVisualizerBackground") == 1) {
 
                 lRenderer.enabled = true;
 
             }
 
             //Obtain the samples from the frequency bands of the attached AudioSource  
             aSource.GetSpectrumData (this.samples, 0, FFTWindow.Hamming);  
 
             //For each sample  
             for (int i = 0; i < samples.Length; i++) {  
                 /*Set the cubePos Vector3 to the same value as the position of the corresponding 
              * cube. However, set it's Y element according to the current sample.*/  
 
                 cubePos.Set (cubesTransform [i].position.x, (Mathf.Sqrt (samples [i] * 20000)) * 3 + 2, cubesTransform [i].position.z);
 
                 //If the new cubePos.y is greater than the current cube position  
 
                 cubesTransform [i].position = cubePos;  
 
                 /*Set the position of each vertex of the line based on the cube position. 
              * Since this method only takes absolute World space positions, it has 
              * been subtracted by the current game object position.*/  
                 if (lRenderer.enabled == true) {
                     lRenderer.SetPosition (i, cubePos);
                 }
 
             }
 
         } else {
 
             if (lRenderer.enabled == true) {
 
                 lRenderer.enabled = false;
 
             }
 
         }
 
         if (PlayerPrefs.GetInt("UseVisualizerBackground") == 1) {
 
             lRenderer.enabled = true;
 
             Debug.Log ("using visualizer");
 
         }
 
     }  
 
 }  


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
1

Answer by SniperEvan · Oct 24, 2015 at 05:56 AM

Unity has built in functions Lerp and Slerp (spherical lerp, gives smoother end points). I would experiment with those first.

If that doesn't suit your needs I would try storing the last few values in an array and taking the average.

Let me know if that helps / you need more information.

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 TheShadyColombian · Oct 24, 2015 at 02:12 PM 0
Share

How would I go about using the lerp and slerp functions?

avatar image TheShadyColombian · Oct 29, 2015 at 03:04 AM 0
Share

@SniperEvan How can I use Lerp and Slerp in this situation?

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

30 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Waiting for a method to signal it is done before launching it again 1 Answer

Initialising List array for use in a custom Editor 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