Hi there, I found a code on this forum that can make a standard unity plane (10 segments each side) wave like a sea. Unfortunately if i make a plane with twice as much vertices, the code doesn't create the same effect. Sound logical, but is there anyone who knows how to fix or who has a better code? And how could Change the direction of the waves? Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class WaterController : MonoBehaviour { public float Scale = 0.1f; public float Speed = 1.0f; public float NoiseStrength = 1f; public float NoiseWalk = 1f; private Vector3[] _baseHeight; private Mesh _mesh; public MeshCollider MC; private void Start() { //the variable MC (MeshCollider) is dragger on the varable via the editor _mesh = GetComponent<MeshFilter>().mesh; } void Update() { 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(); MC.sharedMesh = _mesh; } } You can see it on the right of the image (unity plane) VS left plane (plane with more vertices).
I wouldn't use that code. It'd be way faster to do this with a shader anyways. But if you do want to do it on the CPU use this mesh modifier system. It's really easy. You could stack a Trig (for a sin wave) and Perlin noise modifier.
The OP code is just applying a sign wave to the height of the mesh vertices. It's pretty basic procedural mesh stuff. Yes. But a shader doesn't actually modify the mesh, just its appearance. So it might not actually achieve everything you need. But if the effect is purely cosmetic, then shaders are definely the way to go.
Yea, just wanna make sure OP doesn't duplicate 30 of them in a line to make a level haha. The mesh modifier system I linked does let you split the calculations over multiple frames which is pretty cool. You could have the waves running at half the framerate while letting your main game run a good bit faster.