How to simulate waves in Unity?

How is it possible to simulate water in our game?

I want to make rivers and lakes for my game but I don't know how to make the waves and make the water look like it's moving.

How would I do it?

• There are three separate questions here which you should ask separately: 1. how to make water which looks good (material and shader settings), 2. how to generate waves in a mesh (this is the only part of the question which already got an answer) and 3. how to change an animation from walking to swimming when a characters enters water (you might want to add if you are talking about a sprite or a rigged 3d model). Jul 30 '16 at 16:09
• @Philipp thanks philipe for your comment. I hope you have good answer for it Jul 30 '16 at 16:13
• I might have an answer from one of these questions, but I won't consider writing an answer unless you create it as a new question and add sufficient details. Jul 30 '16 at 16:16

https://docs.unity3d.com/ScriptReference/Mesh-vertices.html

Returns a copy of the vertex positions or assigns a new vertex positions array. The number of vertices in the mesh is changed by assigning a vertex array with a different number of vertices. Note that if you resize the vertex array then all other vertex attributes (normals, colors, tangents, UVs) will be automatically resized too. RecalculateBounds will automatically be invoked if no vertices have been assigned to the mesh when setting the vertices.

this is sample wave simulate by using Mesh vertices

using UnityEngine;
using System.Collections;

public class WaveGen : MonoBehaviour
{
public float scale = 0.1f;
public float speed = 1.0f;
public float noiseStrength = 1f;
public 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();
}
}


1.wave in 2D game

you can easily create wave based on this gif in 3D

2.wave in 3d game

Unfortunately you can not simulate water because at any moment millions of particle to be simulated because game is Realtime(while you can do it for animations by simulating likeRealflow because there is enough time to calculate rendering). It will create lag in your game ,I prefer you use shader in your game.

Shaders are small scripts that contain the mathematical calculations and algorithms for calculating the colour of each pixel rendered, based on the lighting input and the Material configuration. https://docs.unity3d.com/Manual/Shaders.html

Scrolling textures by modifying UV values

Water surface ripple effect for Unity

• thanks for your answer. but what about river? if I want a certain shape of river. should I beuild it in 3d app and import it? is there anyway to make mesh in unity? im going to accept your answer Jul 30 '16 at 16:12
• I using real flow for simulating water but I don't prefer it in unity because of many processing and calculating same rendering by Vray.I am sure that many game use basic simulating water for example battlefield 4 used many water shader instead of simulating it. Jul 30 '16 at 17:44