1. 程式人生 > >unity製作捲動的波浪

unity製作捲動的波浪

using UnityEngine;
using System.Collections;

public class WaterWave : MonoBehaviour
{
    public float scale = 0.5f;
    public float speed = 1f;
    public bool isMultiply = false;

    private Mesh mesh;
    private Vector3[] baseVertex;
    private Vector3[] nowVertex;

    void Start()
    {
        mesh = GetComponent<MeshFilter>().mesh;
        baseVertex = mesh.vertices;
        nowVertex = mesh.vertices;
    }

    void Update()
    {
        for (int i = 0; i < baseVertex.Length; i++)
        {
            nowVertex[i] = baseVertex[i];
            if (isMultiply)
            {
                nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x + baseVertex[i].z) * scale;
            }
            else
            {
                nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x) * scale
                    +
                    Mathf.Sin(Time.time * speed + baseVertex[i].z) * scale;
            }
        }
        mesh.vertices = nowVertex;
    }
}