Search Unity

Creating ocean waves of different widths using Mathf.Sin()

Discussion in 'Scripting' started by gsus725, Jan 29, 2015.

  1. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250


    Code (csharp):
    1.  
    2. var scale=5f;
    3. var speed=1f;
    4. private var base_height:Vector3[];
    5.  
    6. function Update(){
    7.     var mesh=GetComponent(MeshFilter).mesh;
    8.     if(!base_height) base_height=mesh.vertices;
    9.  
    10.     var vertices=new Vector3[base_height.Length];
    11.     for(var i=0;i<vertices.Length;i++){
    12.         var vertex=base_height[i];
    13.         vertex.y+=Mathf.Sin(Time.time * speed + vertex.x) * scale;
    14.         vertices[i]=vertex;
    15.         }
    16.  
    17.     mesh.vertices=vertices;
    18.     mesh.RecalculateNormals();
    19.     }
    20.  
    This code sends a wave through a plane that I am using as an ocean. I need to be able to change the frequency of the waves (wavelength), can someone tell me how?

    I need them to be higher frequency. I don't understand sine waves yet, I've tried to learn and will continue to, but right now it eludes me.
     
    Last edited: Jan 29, 2015
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. gsus725

    gsus725

    Joined:
    Aug 23, 2010
    Posts:
    250
    Thanks it works perfect although I don't understand how all the arguments in Mathf.Sin() come together to create these effects.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Magic