Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED]Change Sin Wave Parameters With Offset?

Discussion in 'Scripting' started by keenanwoodall, Jul 2, 2015.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    I'm looping through a mesh's vertices and want to move their x position via a sin wave. I'm trying to use the vertices' y position to offset the sin wave for each vertice based on it's height. This causes the frequency of the wave to go up and down. Here's my code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Linq;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. [RequireComponent(typeof(MeshFilter))]
    8. public class Wave : MonoBehaviour
    9. {
    10.     //--VARIABLES--
    11.         //-Public-
    12.             //Parameter
    13.     //[Range(0f, 5f)]
    14.     public float amplitude = 2f;
    15.     //[Range(0f, 5f)]
    16.     public float frequency = 0.5f;
    17.  
    18.         //-Private-
    19.             //References
    20.     Mesh mesh;
    21.             //Data
    22.     List<Vector3> startVertices = new List<Vector3>();
    23.     List<Vector3> vertices = new List<Vector3>();
    24.  
    25.     float phase;
    26.     float oldFrequency;
    27.  
    28.     //--METHODS--
    29.         //-Initialization-
    30.     void Awake()
    31.     {
    32.         mesh = GetComponent<MeshFilter>().mesh;
    33.         startVertices = mesh.vertices.ToList();
    34.         vertices = mesh.vertices.ToList();
    35.  
    36.         oldFrequency = frequency;
    37.     }
    38.         //-Repeating-
    39.     void Update()
    40.     {
    41.         for(int i = 0; i < vertices.Count; i++)
    42.             vertices[i] = new Vector3(AdvancedSin(vertices[i].y), 0f, 0f) + startVertices[i];
    43.         mesh.vertices = vertices.ToArray();
    44.     }
    45.  
    46.     float AdvancedSin(float y)
    47.     {
    48.         if(frequency != oldFrequency)
    49.             CalculateNewFrequency();
    50.         return Mathf.Sin(Time.time * oldFrequency  * y + phase) * amplitude;
    51.     }
    52.     void CalculateNewFrequency()
    53.     {
    54.         float current = (Time.time * oldFrequency + phase) % (2f * Mathf.PI);
    55.         float next = (Time.time * frequency) % (2f * Mathf.PI);
    56.         phase = current - next;
    57.         oldFrequency = frequency;
    58.     }
    59. }
    60.  
     
  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    597
    Figured it out.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Linq;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. [RequireComponent(typeof(MeshFilter))]
    8. public class Wave : MonoBehaviour
    9. {
    10.     //--VARIABLES--
    11.         //-Public-
    12.             //Parameter
    13.     public float amplitude = .1;
    14.     public float frequency = 0.5f;
    15.             //Enumerations
    16.         //-Private-
    17.             //References
    18.     Mesh mesh;
    19.             //Data
    20.     List<Vector3> startVertices = new List<Vector3>();
    21.     List<Vector3> vertices = new List<Vector3>();
    22.    
    23.     float phase;
    24.     float oldFrequency;
    25.  
    26.     //--METHODS--
    27.         //-Initialization-
    28.     void Awake()
    29.     {
    30.         mesh = GetComponent<MeshFilter>().mesh;
    31.         startVertices = mesh.vertices.ToList();
    32.         vertices = mesh.vertices.ToList();
    33.  
    34.         oldFrequency = frequency;
    35.     }
    36.         //-Repeating-
    37.     void Update()
    38.     {
    39.         for(int i = 0; i < vertices.Count; i++)
    40.             vertices[i] = new Vector3(AdvancedSin(startVertices[i].y), 0f, 0f) + startVertices[i];
    41.         mesh.vertices = vertices.ToArray();
    42.     }
    43.  
    44.     float AdvancedSin(float y)
    45.     {
    46.         if(frequency != oldFrequency)
    47.             CalculateNewFrequency();
    48.         return Mathf.Sin(Time.time * oldFrequency + phase + y) * amplitude;
    49.     }
    50.     void CalculateNewFrequency()
    51.     {
    52.         float current = (Time.time * oldFrequency + phase) % (2f * Mathf.PI);
    53.         float next = (Time.time * frequency) % (2f * Mathf.PI);
    54.         phase = current - next;
    55.         oldFrequency = frequency;
    56.     }
    57. }