Unity Note - Project10 Wave
coconutnut

尝试做个波浪动画,最后效果:


strip做成Prefab

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class Strip : MonoBehaviour
{
public void Activate(float waitSecond, float moveDistance, float moveDuration)
{
StartCoroutine(ActivateMovement(waitSecond, moveDistance, moveDuration));
}

private IEnumerator ActivateMovement(float waitSecond, float moveDistance, float moveDuration)
{
// wait for some time
yield return new WaitForSeconds(waitSecond);

// set movement
DOTween.Sequence()
.Append(transform.DOMoveY(moveDistance, moveDuration).SetEase(Ease.InOutSine))
.SetLoops(-1,LoopType.Yoyo);
}
}

加一个controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StripController : MonoBehaviour
{
// object setting
[SerializeField] private int stripNum = 10;
[SerializeField] private Vector3 stripScale = new Vector3(0.2f,5,0.01f);
[SerializeField] private Vector3 stripInterval = new Vector3(0.25f,0,0);

// movement setting
[SerializeField] private float moveDistance = 2f;
[SerializeField] private float moveDuration = 2f;
[SerializeField] private float moveInterval = 0.1f;

private Strip stripPrefab;
private Strip[] strips;

void Start()
{
stripPrefab = Resources.Load<Strip>("strip");
CreateStrips();
}

private void CreateStrips()
{
strips = new Strip[stripNum];
for(int i=0; i<stripNum; i++){
// create cube
Strip obj = Instantiate(stripPrefab);
// set position
obj.transform.position = transform.position + stripInterval * i;
obj.transform.localScale = stripScale;
// activate movement
obj.Activate(moveInterval*(i+5), moveDistance, moveDuration);
// add to array
strips[i] = obj;
}
}
}

奇怪的一点,前几个条的起始时间总是有点问题,暂时找不出来为什么,先把所有起始时间往后挪一点 MARK

这样就没问题了


关于WaitForSeconds()

https://forum.unity.com/threads/waitforseconds-in-c.75411/

1
2
3
4
5
6
7
8
9
10
11
12
13
private void executeWait()
{
Debug.Log("wait start");
StartCoroutine(Wait(20.0f));
Debug.Log("This gets called after starting the Coroutine");
}

private IEnumerator Wait(float seconds)
{
Debug.Log("waiting");
yield return new WaitForSeconds(seconds);
Debug.Log("wait end");
}

现在这个波浪效果一个问题是DOTWeen消耗太大

也许还是直接用波浪形物体平移?


一个很喜欢的chrome插件几枝是用noise做的波浪,试一下Mathf.PerlinNoise()

参考:

https://blog.csdn.net/FumikiSAMA/article/details/80212432

https://docs.unity3d.com/ScriptReference/Mathf.PerlinNoise.html

网格拓扑绘制图形

https://www.jianshu.com/p/1f79e33308a6

试着画了一个四面体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Vector3[] vertices = new Vector3[4];
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3((float)System.Math.Sqrt(3), 1, 0);
vertices[2] = new Vector3(0, 2, 0);
vertices[3] = new Vector3((float)System.Math.Sqrt(1/3), 1, (float)System.Math.Sqrt(8/3));

int[] triangle = new int[12] {0,2,1,0,1,3,1,2,3,2,0,3};

// get mesh
Mesh mesh = GetComponent<MeshFilter>().mesh;

// clear original mesh
mesh.Clear();

// set new vertices
mesh.vertices = vertices;

// set new triangles
mesh.triangles = triangle;

// calculate mesh
mesh.RecalculateNormals();

下次用这个画波浪试试