Unity3DGame学习笔记:粒子系统(homework9)

本文记录了使用Unity3D制作粒子光环的过程。通过设置粒子系统的参数,调整粒子的半径和数量,实现环状分布。在Start函数中初始化粒子位置,Update函数中更新粒子位置以产生动态效果。为了实现光圈缩放,引入了新的半径数组,并通过监听鼠标点击切换缩放状态。最后,通过渐变的方式优化了缩放过程,达到平滑过渡的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次的任务是制作一个粒子光环

首先需要创建一个粒子系统,粒子系统参数设置如下



为了让粒子呈环状分布,我们需要设置其最大和最小半径,还需要设定粒子的数量

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);
	}
		
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值