Search Unity

Water waves c#

Discussion in 'Scripting' started by TitusMachine_, Jun 5, 2017.

  1. TitusMachine_

    TitusMachine_

    Joined:
    Apr 24, 2017
    Posts:
    13
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class WaterController : MonoBehaviour {
    6.  
    7.     public float Scale = 0.1f;
    8.     public float Speed = 1.0f;
    9.     public float NoiseStrength = 1f;
    10.     public float NoiseWalk = 1f;
    11.     private Vector3[] _baseHeight;
    12.     private Mesh _mesh;
    13.     public MeshCollider MC;
    14.  
    15.     private void Start()
    16.     {
    17.         //the variable MC (MeshCollider) is dragger on the varable via the editor
    18.         _mesh = GetComponent<MeshFilter>().mesh;
    19.     }
    20.     void Update()
    21.     {
    22.         if (_baseHeight == null)
    23.             _baseHeight = _mesh.vertices;
    24.  
    25.         Vector3[] vertices = new Vector3[_baseHeight.Length];
    26.         for (int i = 0; i < vertices.Length; i++)
    27.         {
    28.             Vector3 vertex = _baseHeight[i];
    29.             vertex.y += Mathf.Sin(Time.time * Speed + _baseHeight[i].x + _baseHeight[i].y + _baseHeight[i].z) * Scale;
    30.             vertex.y += Mathf.PerlinNoise(_baseHeight[i].x + NoiseWalk, _baseHeight[i].y + Mathf.Sin(Time.time * 0.1f)) * NoiseStrength;
    31.             vertices[i] = vertex;
    32.         }
    33.         _mesh.vertices = vertices;
    34.         _mesh.RecalculateNormals();
    35.  
    36.         MC.sharedMesh = _mesh;
    37.     }
    38. }
    39.  
    You can see it on the right of the image (unity plane) VS left plane (plane with more vertices).
     

    Attached Files:

    Last edited: Jun 5, 2017
  2. Caedriel

    Caedriel

    Joined:
    Jun 19, 2015
    Posts:
    8
    Hi , i am trying to understand the code & was wondering if you can link me to its original author ?
     
  3. fivestone

    fivestone

    Joined:
    Dec 17, 2013
    Posts:
    6
    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.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  5. fivestone

    fivestone

    Joined:
    Dec 17, 2013
    Posts:
    6
    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.
     
    Kiwasi likes this.