-1
\$\begingroup\$

I want to simple wave-like animation using mesh. I thought that it would look like a wave if there would be a point at the middle of the mesh and change z axis of that point, and after this I change points at around of middle point one by one. However, it didn't happen.

What should I do to make a wave-like mesh animation?

My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]

public class Gridd : MonoBehaviour
{
    public Mesh mesh;
    public float axisCounter;
    public Vector3[] vertices;
    public int xSize, ySize;
    point nokta;

    int sayac = 0;

    private void Awake ()
    {
        mesh = new Mesh();
        Generate();
    }

    private void Generate ()
    {
        _generate();
    }

    private void OnDrawGizmos ()
    {
        draw();
    }

    void Start()
    {
        nokta = new point(1,1,xSize,ySize,vertices);
    }

    void Update()
    {
        createMesh();
        StartCoroutine(destroy());
    }

    IEnumerator destroy()
    {
        float zAxis = 0;
        while(true)
        {
            while(zAxis>=-axisCounter)
            {
                if(zAxis<=-axisCounter)
                {
                    break;
                }
                zAxis-=0.01f;
                nokta.func(xSize/2,ySize/2,zAxis);

                yield return new WaitForSeconds(0.01f);
            }

            while(zAxis<=axisCounter)
            {
                if(zAxis>=axisCounter)
                {
                    break;
                }
                zAxis+=0.01f;
                nokta.func(xSize/2,ySize/2,zAxis);

                yield return new WaitForSeconds(0.01f);
            }
        }
    }

    /*************************************************************************/

    void _generate()
    {
        mesh = new Mesh();
        GetComponent<MeshFilter>().mesh = mesh;
        vertices = new Vector3[(xSize + 1) * (ySize + 1)];
        for (int y = 0; y <= ySize; y++)
        {
            for (int x = 0; x <= xSize; x++)
            {
                vertices[sayac] = new Vector3(x, y);
                sayac++;
            }
        }
        sayac=0;
    }

    void createMesh()
    {
        mesh.vertices = vertices;
        int[] triangles = new int[xSize*xSize*6];
        for(int y=0;y<ySize;y++)
        {
            for(int x=0;x<xSize;x++)
            {
                triangles[sayac] = (xSize+1)*y+x;
                sayac++;
                triangles[sayac] = (xSize+1)*(y+1)+x;
                sayac++;
                triangles[sayac] = (xSize+1)*y+x+1;
                sayac++;
                triangles[sayac] = (xSize+1)*y+x+1;
                sayac++;
                triangles[sayac] = (xSize+1)*(y+1)+x;
                sayac++;
                triangles[sayac] = (xSize+1)*(y+1)+x+1;
                sayac++;
            }
        }
        sayac=0;
        mesh.triangles = triangles;
    }

    void draw()
    {
        if (vertices == null)
        {
            return;
        }
        Gizmos.color = Color.black;
        for (int i = 0; i < vertices.Length; i++)
        {
            Gizmos.DrawSphere(vertices[i], 0.01f);
        }
    }
}

/*****************************************************************************/

public class point
{
    public int x;
    public int y;
    public int xSize,ySize;
    public int[,] _nokta;
    public int[,] dalga;
    int atlayici;
    int kolaynokta;
    int kolaynoktax,kolaynoktay;
    public Vector3[] vertices;
    bool buldumu = false;

    public point(int x,int y,int xSize,int ySize,Vector3[] vertices)
    {
        this.x = x;
        this.y = atlayici*y;
        this.xSize = xSize;
        this.ySize = ySize;
        this.vertices = vertices;
        this.atlayici = xSize+1;
        this._nokta = new int[xSize,ySize];
        this.kolaynokta = (xSize+1)*y+x;
        this.dalga = new int[ySize/2+1,xSize*ySize];
    }

    public void func(int x,int y,float z)
    {
        vertices[pointFinder(x,y)] = new Vector3(x,y,-z/2f);
        subWaves(x,y,z);
    }

    public void subWaves(int x,int y,float z)
    {
        if(!buldumu)
        {
            find(x,y,z);
        }
        int counter = 8;
        for(int a=0;a<ySize/2;a++)
        {
            for(int b=0;b<counter;b++)
            {
                vertices[dalga[a,b]] = new Vector3(vertices[dalga[a,b]].x,vertices[dalga[a,b]].y,_math(a,z));
            }
            counter+=8;
        }           
    }

    /*************************************************************************/

    void find(int x,int y,float z)
    {
        int katman = 2;
        int wavex = x;
        int wavey = y;
        int _x=0;
        for(int a=0;a<ySize/2;a++)
        {
            wavex--;
            wavey++;        
            int xarray=0;
            int yarray=0;
            for(int c=0;c<katman;c++)
            {
                dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);       
                xarray++;
                _x++;
            }
            for(int c=0;c<katman;c++)
            {
                dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
                yarray--;
                _x++;
            }
            for(int c=0;c<katman;c++)
            {
                dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
                xarray--;
                _x++;
            }
            for(int c=0;c<katman;c++)
            {
                dalga[a,_x] = pointFinder(wavex+xarray,wavey+yarray);
                yarray++;
                _x++;
            }
            katman+=2;
            _x=0;
        }
        buldumu = true;
    }

    int pointFinder(int x,int y)
    {
        int nokta = (xSize+1)*y+x;
        return nokta;
    }

    float _math(int a,float z)
    {
        z*=-1;
        float deger;
        deger=z/(1.25f+(a+1));
        return deger;
    }
}

Below image (GIF) of result that I achieved:

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Well that thing moves, does it not? I barely skimmed the code while formatting it, yet from the animation I think it is a problem of amplitude and timing. \$\endgroup\$ – Theraot Jul 16 '19 at 1:15
0
\$\begingroup\$

maybe instead of coding, you should make an animation and loop it. This could be done in a 3rd party program, like blender, and then exported to unity

\$\endgroup\$
  • \$\begingroup\$ Do most of indie game developers use 3rd party programs to make kind of this wave things ?I thought that to i should learn mesh system in unity to make wave animation,but now i realized making wave animation is really hard and require math and physics knowledge wery well.Main problem is that i don't have great math background,How can i become good game developer. \$\endgroup\$ – ShooterLens Aim Jul 16 '19 at 9:48
  • 1
    \$\begingroup\$ @ShooterLensAim Programming is nothing but math, you're not going to get very far without it. \$\endgroup\$ – Draco18s no longer trusts SE Jul 16 '19 at 14:55
  • \$\begingroup\$ İ shared my gif,i think it may help. \$\endgroup\$ – ShooterLens Aim Jul 16 '19 at 18:22
  • \$\begingroup\$ You don't need to use a 3rd party program,Unity has a good animation setup. I'm just suggesting that you could use an animation versus coding, as it seems more straight forward in this particular case. Also, game math doesn't have to be too complicated. Often times a simple math setup can give you a good result \$\endgroup\$ – Millard Jul 21 '19 at 17:51
0
\$\begingroup\$

I think you might appreciate the answers to your question in the catlikecoding blog. https://catlikecoding.com/unity/tutorials/flow/waves/

i don't know who makes this, but their content is amazing for procedural generation and they have a patreon if you want to give them a hand.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.