Voice-responsive procedural mesh vertices messed up when script is running

Hello everyone!
It’s my first time to write a thread… I need help :slight_smile:

I’m making a real-time voice reponsive mesh,
I’d like to make those mesh vertices move by audio data from microphone.
So far, I made a procedural speare mesh and successfully made them moving.

But, for some reason, after I activate my script(voice to mesh), vertices of procedural speare mesh are messed up. I attached screenshot.

<IMAGE 1> Perfect procedural spear mesh without running voice to mesh script

messed up spear mesh after activate script

wireframe view

I don’t understand why this is happening…
here’s my c# code.

using System.Collections;
using UnityEngine;


public class VertAnimation : MonoBehaviour {

    private Mesh mesh;
    private Vector3[] verts = RoundedCube.vertices;
    private Vector3 vertPos;
    private GameObject[] reactor;

    //public GameObject AngerCrystal;
    private const string TAG_REACTORS = "ReactingSound";

    // Use this for initialization
    void OnEnable()
    {

    }
    void Start()
    {
        mesh = GetComponent<MeshFilter>().mesh;
        verts = mesh.vertices;
        foreach (Vector3 vert in verts)
        {
            vertPos = transform.TransformPoint(vert);
            GameObject reactor = new GameObject(TAG_REACTORS);

            reactor.transform.position = vertPos;
            reactor.transform.parent = transform;
            reactor.tag = TAG_REACTORS;

           // reactor.AddComponent<VertHandleGizmo>()._parent = this;
        }
    }
    void FixedUpdate()
    {
        reactor = GameObject.FindGameObjectsWithTag(TAG_REACTORS);

        for (int i = 0; i < verts.Length; i++)
        {

            verts[i] = reactor[i].transform.localPosition - RoundedCube.MeshSize; // ALIGN THE CENTER OF VIBE TO 0,0,0(HALF OF CUBE SIZE)
            verts[i] = verts[i] * (AudioVis._samples[i] + 0.1f) * 1000;
           
           
        }

        mesh.vertices = verts;
        // mesh.RecalculateBounds();
        //  mesh.RecalculateNormals();

       // if(Input.GetKey(KeyCode.S) == true)
       // {
       //     SaveVertsPos();
       // }
    }
   

    /*void SaveVertsPos()
    {
        Vector3[] VertsPos = new Vector3[verts.Length];
        for (int i = 0; i < verts.Length; i++)
        {
            reactor[i] = AngerCrystal;
            VertsPos[i] = AngerCrystal.transform.position;
            Debug.Log(VertsPos[1].ToString("F2"));
        }
    }*/
}

Thank you for reading.

Use code tags. At least one of your lines got mangled because an [ i ] is in there, which it interpreted as italics. Also, it just makes the code easier to read and follow in general.

Oops, thanks man. I’ll fix it right now

As for your algorithm itself, for starters, I’m extremely puzzled why you create hundreds of GameObjects, when as far as I can tell, all they’re doing is storing positions. Just use an array of Vector3’s instead - much fewer opportunities for errors to creep in, and much more efficient in terms of memory and initialization time. Plus, if you ever decide to add a second sphere, doing it this way will totally mess everything up.

And in general, IF you use FindGameObjectsWithTag, call it ONCE and store the result - not every FixedUpdate.

Also, if you’re not using physics, you probably don’t want to use FixedUpdate. Since this is a purely visual effect, LateUpdate is the most appropriate place for it.

I think the issue you’re seeing is that you subtract RoundedCube.MeshSize - that’s going to make everything be offset weirdly, and that’s probably causing the weird vertex positions. The sphere’s vertices are already centered around (0,0,0) in local space, which I assume is what you want.