- Home /
How to generate Stormy Waves in the BasicWaterPlane?
Edit: Using the basic Water4 assets included in the default Environment Asset will create waves
I am making a game where ships battle each other, kind of like Colonial kart World on Pacogames, but all of the scripts I've tested only create small lapping waves on the shoreline. I'm using an old script call WaveGen changed from java to C# by @RabidCabbage, here's the script:
using UnityEngine;
using System.Collections;
public class WaveGen : MonoBehaviour
{
float scale = 0.1f;
float speed = 1.0f;
float noiseStrength = 1f;
float noiseWalk = 1f;
private Vector3[] baseHeight;
void Update () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
if (baseHeight == null)
baseHeight = mesh.vertices;
Vector3[] vertices = new Vector3[baseHeight.Length];
for (int i=0;i<vertices.Length;i++)
{
Vector3 vertex = baseHeight[i];
vertex.y += Mathf.Sin(Time.time * speed+ baseHeight[i].x + baseHeight[i].y + baseHeight[i].z) * scale;
vertex.y += Mathf.PerlinNoise(baseHeight[i].x + noiseWalk, baseHeight[i].y + Mathf.Sin(Time.time * 0.1f) ) * noiseStrength;
vertices[i] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
}
}
All this does is create lapping waves on the shoreline, which as useful for future survival games that I may make, but I need stormy waves, and the possibility to adjust the height (optional). Any help is appreciated, I use an older version of Unity (Unity 5.3.3) but most scripts work. Thanks, Rusty
Answer by KittenSnipes · Apr 26, 2018 at 07:55 AM
Your answer is in your face lol. I think I adjusted it well enough to look stormy. All of those variables just need to be public so you can adjust them in the editor:
public float scale = 0.2f;
public float speed = 6.0f;
public float noiseStrength = 2f;
public float noiseWalk = 2f;
private Vector3[] baseHeight;
void Update () {
Mesh mesh = GetComponent<MeshFilter>().mesh;
if (baseHeight == null)
baseHeight = mesh.vertices;
Vector3[] vertices = new Vector3[baseHeight.Length];
for (int i=0;i<vertices.Length;i++)
{
Vector3 vertex = baseHeight[i];
vertex.y += Mathf.Sin(Time.time * speed+ baseHeight[i].x + baseHeight[i].y + baseHeight[i].z) * scale;
vertex.y += Mathf.PerlinNoise(baseHeight[i].x + noiseWalk, baseHeight[i].y + Mathf.Sin(Time.time * 0.1f) ) * noiseStrength;
vertices[i] = vertex;
}
mesh.vertices = vertices;
mesh.RecalculateNormals();
}