WaterWave.cs 964 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 
  2. using UnityEngine;
  3. using System.Collections;
  4. public class WaterWave : MonoBehaviour {
  5. public float scale = 0.5f;
  6. public float speed = 1f;
  7. public bool isMultiply = false;//若为true则波浪数变多
  8. private Mesh mesh;
  9. private Vector3[] baseVertex;
  10. private Vector3[] nowVertex;
  11. // Use this for initialization
  12. void Start ()
  13. {
  14. mesh = GetComponent<MeshFilter>().mesh;
  15. baseVertex = mesh.vertices;
  16. nowVertex = mesh.vertices;
  17. }
  18. // Update is called once per frame
  19. void Update ()
  20. {
  21. for (int i = 0; i < baseVertex.Length; i++)
  22. {
  23. nowVertex[i] = baseVertex[i];
  24. if (isMultiply)
  25. {
  26. nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x + baseVertex[i].z) * scale;
  27. }
  28. else
  29. {
  30. nowVertex[i].y += Mathf.Sin(Time.time * speed + baseVertex[i].x) * scale +
  31. Mathf.Sin(Time.time * speed + baseVertex[i].z) * scale;
  32. }
  33. }
  34. mesh.vertices = nowVertex;
  35. }
  36. }