我是靠谱客的博主 忧心黑猫,这篇文章主要介绍Unity 抛物线,直线,Sine曲线等,现在分享给大家,希望可以做个参考。

1.用粒子制作抛物线。

1.创建一个枚举

复制代码
1
2
3
4
5
6
public enum FunctionOption { Linear, Exponential, Parabola, Sine }

2.创建静态方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//直线 private static float Linear (float x) { return x; } //曲线 private static float Exponential (float x) { return x * x; } //抛物线 private static float Parabola (float x){ x = 2f * x - 1f; return x * x; } //Sine 曲线 可以通过相位来控制Shader水的流动,有兴趣的可以去实现 private static float Sine (float x){ return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x + Time.timeSinceLevelLoad); }

3.创建委托函数

复制代码
1
2
3
4
5
6
7
8
private delegate float FunctionDelegate (float x); private static FunctionDelegate[] functionDelegates = { Linear, Exponential, Parabola, Sine }; public FunctionOption function;

4.创建点

复制代码
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
private void CreatePoints () { currentResolution = resolution; points = new ParticleSystem.Particle[resolution]; float increment = 1f / (resolution - 1); for (int i = 0; i < resolution; i++) { float x = i * increment; points[i].position = new Vector3(x, 0f, 0f); points[i].color = new Color(x, 0f, 0f); points[i].size = 0.1f; } } void Update () { if (currentResolution != resolution || points == null) { CreatePoints(); } FunctionDelegate f = functionDelegates[(int)function]; for (int i = 0; i < resolution; i++) { Vector3 p = points[i].position; p.y = f(p.x); points[i].position = p; Color c = points[i].startColor; c.g = p.y; points[i].startColor = c; } GetComponent<ParticleSystem>().SetParticles(points, points.Length); }

5.全部代码如下 ,并把他放在粒子物体上,不过这个一直是在Update里面执行,所以性能会比较耗性能,这
主要测试功能用的。

复制代码
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine; public class Test : MonoBehaviour { public enum FunctionOption { Linear, Exponential, Parabola, Sine } private delegate float FunctionDelegate (float x); private static FunctionDelegate[] functionDelegates = { Linear, Exponential, Parabola, Sine }; public FunctionOption function; [Range(10, 100)] public int resolution = 10; private int currentResolution; private ParticleSystem.Particle[] points; private void CreatePoints () { currentResolution = resolution; points = new ParticleSystem.Particle[resolution]; float increment = 1f / (resolution - 1); for (int i = 0; i < resolution; i++) { float x = i * increment; points[i].position = new Vector3(x, 0f, 0f); points[i].color = new Color(x, 0f, 0f); points[i].size = 0.1f; } } void Update () { if (currentResolution != resolution || points == null) { CreatePoints(); } FunctionDelegate f = functionDelegates[(int)function]; for (int i = 0; i < resolution; i++) { Vector3 p = points[i].position; p.y = f(p.x); points[i].position = p; Color c = points[i].startColor; c.g = p.y; points[i].startColor = c; } GetComponent<ParticleSystem>().SetParticles(points, points.Length); } private static float Linear (float x) { return x; } private static float Exponential (float x) { return x * x; } private static float Parabola (float x){ x = 2f * x - 1f; return x * x; } private static float Sine (float x){ return 0.5f + 0.5f * Mathf.Sin(2 * Mathf.PI * x + Time.timeSinceLevelLoad); } }

最后

以上就是忧心黑猫最近收集整理的关于Unity 抛物线,直线,Sine曲线等的全部内容,更多相关Unity内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部