In this script where I create waves, I am updating the waves. My goal is to keep the player’s ship fixed at the x and z coordinates and determine the direction and speed of the waves relative to the ship. In this part of the script, the speed factor changes the wave speed, but when I increase the speed, the waves first move forward and then accelerate. Then, when I decrease the speed, the waves move backward and slow down. I only want to change the speed of the waves, but I can’t figure out how to do it. Could you help me?
Video link: Waves.mp4 - Google Drive
void UpdateChunkMesh(GameObject chunk)
{
var meshFilter = chunk.GetComponent<MeshFilter>();
if (meshFilter == null) return;
var mesh = meshFilter.mesh;
var verts = mesh.vertices;
for (int i = 0; i < verts.Length; i++)
{
var vertex = verts[i];
float y = 0f;
// Apply wave calculation
for (int o = 0; o < Octaves.Length; o++)
{
if (Octaves[o].alternate)
{
var perl = Mathf.PerlinNoise((vertex.x * Octaves[o].scale.x) / Dimension, (vertex.z * Octaves[o].scale.y) / Dimension) * Mathf.PI * 2f;
y += Mathf.Cos(perl + Octaves[o].speed.magnitude * Time.time) * Octaves[o].height;
}
else
{
var perl = Mathf.PerlinNoise((vertex.x * Octaves[o].scale.x + Time.time * Octaves[o].speed.x) / Dimension, (vertex.z * Octaves[o].scale.y + Time.time * Octaves[o].speed.y) / Dimension) - 0.5f;
y += perl * Octaves[o].height;
}
}
verts[i] = new Vector3(vertex.x, y, vertex.z);
}
mesh.vertices = verts;
mesh.RecalculateNormals();
}
[Serializable]
public struct Octave
{
public Vector2 speed;
public Vector2 scale;
public float height;
public bool alternate;
}