效果图:
这里,我们制作的波浪是通过改变mesh上的顶点来实现的。更准确的说,是改变mesh上顶点的y值,从而形成一种高度变化的效果。
1.通过观察,我们发现每个顶点的y值变化的情况都不一样,因此,很容易想到将顶点的y值与该顶点的x,z值关联起来。
2.通过观察,我们发现第一图的波浪数较少,第二图的波浪数较多,波浪数较小说明各顶点的y值差异较大。可以通过“放大”x,z值来增加不同顶点之间的差异。
- using UnityEngine;
- using System.Collections;
- public class WaterWave : MonoBehaviour {
- public float scale = 0.5f;
- public float speed = 1f;
- public bool isMultiply = false;//若为true则波浪数变多
- private Mesh mesh;
- private Vector3[] baseVertex;
- private Vector3[] nowVertex;
- // Use this for initialization
- void Start ()
- {
- mesh = GetComponent<MeshFilter>().mesh;
- baseVertex = mesh.vertices;
- nowVertex = mesh.vertices;
- }
- // Update is called once per frame
- 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;
- }
- }