我是靠谱客的博主 忧心黑猫,最近开发中收集的这篇文章主要介绍Unity 抛物线,直线,Sine曲线等,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.用粒子制作抛物线。

1.创建一个枚举

public enum FunctionOption {
Linear,
Exponential,
Parabola,
Sine
}

2.创建静态方法

//直线
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.创建委托函数

private delegate float FunctionDelegate (float x);
private static FunctionDelegate[] functionDelegates = {
Linear,
Exponential,
Parabola,
Sine
};
public FunctionOption function;

4.创建点

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里面执行,所以性能会比较耗性能,这
主要测试功能用的。

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 抛物线,直线,Sine曲线等所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部