这次的任务是制作一个粒子光环
首先需要创建一个粒子系统,粒子系统参数设置如下
为了让粒子呈环状分布,我们需要设置其最大和最小半径,还需要设定粒子的数量
public int particleNum = 10000;
public float minRadius = 7.0f;
public float maxRadius = 10.0f;
通过两个float数组记录粒子所处的半径和角度,从而唯一确定某个粒子的位置
public float[] particleAngle;
public float[] particleR;
下面这部分Start函数中的代码在实现粒子环状分布的同时,实现了粒子集中与环的中间而内圈及外圈的分布比较分散
for (int i = 0; i < particleNum; i++) {
float midRadius = (maxRadius + minRadius)/2;
float rate1 = Random.Range(1.0f, midRadius / minRadius);
float rate2 = Random.Range(midRadius / maxRadius, 1.0f);
particleR[i] = Random.Range(minRadius*rate1, maxRadius*rate2);
particleAngle[i] = Random.Range(0.0f, 360.0f);
float rad = particleAngle[i] / 180 * Mathf.PI;
particleRing[i].position = new Vector3(particleR[i] * Mathf.Cos(rad), particleR[i] * Mathf.Sin(rad), 0.0f);
}
除了初始化粒子的位置,还需要在Update函数中不断更新其位置使粒子动起来,通过索引将粒子分为两部分,第一部分顺时针移动,第二部分逆时针移动
for (int i = 0; i < particleNum; i++) {
if (i % 2 == 0) {
particleAngle [i] += (i % level + 1) * speed;
} else {
particleAngle [i] -= (i % level + 1) * speed;
}
particleAngle [i] = particleAngle [i] % 360;
float rad = particleAngle [i] / 180 * Mathf.PI;
particleRing [i].position = new Vector3 (particleR [i] * Mathf.Cos (rad), particleR [i] * Mathf.Sin (rad), 0.0f);
}
目前实现效果如下:
要实现参考网站的光圈缩放效果,主要是要更改粒子分布的半径
在上面我们用float数组particleR[]储存每个粒子的半径,现在要实现缩小,要增加一个数组存放粒子在光圈缩小后的半径
还需要确定缩小后的最大最小半径
public float collectMinRadius = 6.0f;
public float collectMaxRadius = 7.0f;
public float[] collectParticleR;
用一个bool变量collect记录当前光圈的状态,在Update函数中,通过监听鼠标点击改变collect的值
通过collect的值重置粒子的位置
void Update() {
if (Input.GetButtonDown ("fire1")) {
Debug.Log ("mouse down!");
if (collect == true) {
collect = false;
} else {
collect = true;
}
}
if (collect == false) {
for (int i = 0; i < particleNum; i++) {
if (i % 2 == 0) {
particleAngle [i] += (i % level + 1) * speed;
} else {
particleAngle [i] -= (i % level + 1) * speed;
}
particleAngle [i] = particleAngle [i] % 360;
float rad = particleAngle [i] / 180 * Mathf.PI;
particleRing [i].position = new Vector3 (particleR [i] * Mathf.Cos (rad), particleR [i] * Mathf.Sin (rad), 0.0f);
}
} else {
for (int i = 0; i < particleNum; i++) {
if (i % 2 == 0) {
particleAngle [i] += (i % level + 1) * speed;
} else {
particleAngle [i] -= (i % level + 1) * speed;
}
particleAngle [i] = particleAngle [i] % 360;
float rad = particleAngle [i] / 180 * Mathf.PI;
particleRing [i].position = new Vector3 (collectParticleR [i] * Mathf.Cos (rad), collectParticleR [i] * Mathf.Sin (rad), 0.0f);
}
}
particleSystem.SetParticles (particleRing, particleNum);
}
实现效果如下:
缩放效果是顺变的显得非常的突兀,因此我采用新的float数组记录当前的半径,在Update函数中与最终半径进行比较然后递增递减,从而实现缩放时渐变的效果
for (int i = 0; i < particleNum; i++) {
if (collect == true) {
if (currentR [i] > collectParticleR [i]) {
currentR [i] -= (particleR [i] - collectParticleR [i]) / 50;
}
} else {
if (currentR [i] < particleR [i]) {
currentR[i] += (particleR [i] - collectParticleR [i]) / 50;
}
}
if (i % 2 == 0) {
particleAngle [i] += (i % level + 1) * speed;
} else {
particleAngle [i] -= (i % level + 1) * speed;
}
particleAngle [i] = particleAngle [i] % 360;
float rad = particleAngle [i] / 180 * Mathf.PI;
particleRing [i].position = new Vector3 (currentR [i] * Mathf.Cos (rad), currentR [i] * Mathf.Sin (rad), 0.0f);
}
最终实现效果如下:
下面是完整代码:
public class particle : MonoBehaviour {
public ParticleSystem particleSystem;
private ParticleSystem.Particle[] particleRing;
public int particleNum = 10000;
public float minRadius = 8.0f;
public float maxRadius = 11.0f;
public int level = 5;
public float speed = 0.1f;
public bool collect = false;
public float collectMinRadius = 6.5f;
public float collectMaxRadius = 8.0f;
public float[] collectParticleR;
public float[] particleAngle;
public float[] particleR;
public float[] currentR;
void Start () {
particleRing = new ParticleSystem.Particle[particleNum];
particleSystem.maxParticles = particleNum;
particleSystem.Emit(particleNum);
particleSystem.GetParticles(particleRing);
particleAngle = new float[particleNum];
particleR = new float[particleNum];
collectParticleR = new float[particleNum];
currentR = new float[particleNum];
for (int i = 0; i < particleNum; i++) {
float midRadius = (maxRadius + minRadius)/2;
float rate1 = Random.Range(1.0f, midRadius / minRadius);
float rate2 = Random.Range(midRadius / maxRadius, 1.0f);
particleR[i] = Random.Range(minRadius*rate1, maxRadius*rate2);
particleAngle[i] = Random.Range(0.0f, 360.0f);
float rad = particleAngle[i] / 180 * Mathf.PI;
particleRing[i].position = new Vector3(particleR[i] * Mathf.Cos(rad), particleR[i] * Mathf.Sin(rad), 0.0f);
float collectMidRadius = (collectMaxRadius + collectMinRadius) / 2;
float rate3 = Random.Range(collectMinRadius / collectMaxRadius, 1.0f);
collectParticleR[i] = Random.Range(collectMinRadius, collectMaxRadius * rate3);
currentR [i] = particleR [i];
}
particleSystem.SetParticles(particleRing,particleNum);
}
void Update() {
if (Input.GetButtonDown("Fire1")) {
Debug.Log ("collect!");
if (collect == true) {
collect = false;
} else {
collect = true;
}
}
for (int i = 0; i < particleNum; i++) {
if (collect == true) {
if (currentR [i] > collectParticleR [i]) {
currentR [i] -= (particleR [i] - collectParticleR [i]) / 50;
}
} else {
if (currentR [i] < particleR [i]) {
currentR[i] += (particleR [i] - collectParticleR [i]) / 50;
}
}
if (i % 2 == 0) {
particleAngle [i] += (i % level + 1) * speed;
} else {
particleAngle [i] -= (i % level + 1) * speed;
}
particleAngle [i] = particleAngle [i] % 360;
float rad = particleAngle [i] / 180 * Mathf.PI;
particleRing [i].position = new Vector3 (currentR [i] * Mathf.Cos (rad), currentR [i] * Mathf.Sin (rad), 0.0f);
}
particleSystem.SetParticles (particleRing, particleNum);
}
}