0
\$\begingroup\$

I'm creating many GameObjects and each one has a single mesh, created in a script. The problem is that I would like to draw them so that they behave like a single mesh. Is it possible? Or I must combine all the meshes togheter?

enter image description here

Thanks for your help!

Edit: Added the code where I create the mesh and an image to clarify what I did enter image description here

public void createMesh() {
        List<Vector3> vertices = new List<Vector3>();
        List<int> triangles = new List<int>();

        Vector3 _right = rightBase * radiusBase;

        //BASE
        //POC = Point on circumference
        Vector3 axis = Vector3.Cross(_right, fwdBase); //Axis of rotation of the vector _right for the base
        for(int POC = 0; POC < numberOfSections; POC++) {
            vertices.Add(_right);

            _right = Quaternion.AngleAxis(360 / numberOfSections, axis) * _right;
        }

        //TOP
        _right = rightTop * radiusTop;

        axis = Vector3.Cross(_right, fwdTop);
        for(int POC = 0; POC < numberOfSections; POC++) {
            vertices.Add(direction + _right);

            _right = Quaternion.AngleAxis(360 / numberOfSections, axis) * _right;
        }

        //The 2 points at the center of the base and the top
        vertices.Add(Vector3.zero);
        vertices.Add(direction);

        //Triangles
        for(int face = 0; face < numberOfSections - 1; face++) {
            triangles.Add(face);
            triangles.Add(face + numberOfSections);
            triangles.Add(face + 1);

            triangles.Add(face + 1);
            triangles.Add(face + numberOfSections);
            triangles.Add(face + numberOfSections + 1);
        }
        triangles.Add(numberOfSections - 1);
        triangles.Add(2 * numberOfSections - 1);
        triangles.Add(0);

        triangles.Add(0);
        triangles.Add(2 * numberOfSections - 1);
        triangles.Add(numberOfSections);

        //COVER BASE
        for(int POC = 0; POC < numberOfSections - 1; POC++) {
            triangles.Add(2 * numberOfSections);
            triangles.Add(POC);
            triangles.Add(POC + 1);
        }
        triangles.Add(2 * numberOfSections);
        triangles.Add(numberOfSections - 1);
        triangles.Add(0);


        //COVER TOP
        for(int POC = 0; POC < numberOfSections - 1; POC++) {
            triangles.Add(2 * numberOfSections + 1);
            triangles.Add(numberOfSections + POC + 1);
            triangles.Add(numberOfSections + POC);
        }
        triangles.Add(2 * numberOfSections + 1);
        triangles.Add(numberOfSections);
        triangles.Add(2 * numberOfSections - 1);


        getMesh().Clear();
        getMesh().vertices = vertices.ToArray();
        getMesh().triangles = triangles.ToArray();
        getMesh().RecalculateNormals();
        getMesh().RecalculateBounds();
        obj.GetComponent<MeshCollider>().sharedMesh = getMesh();
    }
\$\endgroup\$
  • \$\begingroup\$ Can you show us how you create your meshes? Also, have you considered creating them as a single mesh? This may be substantially more efficient, depending on what kinds of transformations they need to undergo. \$\endgroup\$ – DMGregory May 28 at 22:38
  • \$\begingroup\$ @DMGregory added the code. They don't need to move, I did it with more objects simply because I could get the corresponding gameObject just by raycasting my mouse in world space. \$\endgroup\$ – pava May 29 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.