如题,要求是做物体运动轨迹的拖尾,比如飞机/导弹的尾焰。初次接触,多有不妥请见谅。
效果:

1.新建粒子 particle:

2.设置粒子属性:
注意打勾“自定义”,后面参数如下

3.将粒子拖拽进资源管理器,做成预制体

4.创建粒子的代码,在子弹管理器里面生成子弹后,马上接着生成粒子,对应绑定粒子node进子弹的脚本里
/**创建玩家子弹尾焰
bulletNode: 跟随的子弹节点
nodeID:编号用于缓存对象池
*/
createParticle(bulletNode, nodeID) {
let newParticle = null;
let self = this;
//获取到子弹控件后设置坐标和展示
function setParticle(particleNode) {
// console.log("newParticle:获取到子弹控件后设置坐标和展示", this);
particleNode.active = false;
particleNode.getComponent(cc.ParticleSystem).resetSystem();
particleNode.setPosition(bulletNode.getPosition());
self.gameScene.fireShow(particleNode);
self.particleArr[nodeID] = particleNode;
particleNode.active = true;
bulletNode.getComponent('FlyBullet').setParticle(particleNode);
}
if (this.particlePool == null) {
this.particlePool = new cc.NodePool();
}
// console.log("this--particlePool=", this.particlePool);
if (this.particlePool.size() > 0) { // 通过 size 接口判断对象池中是否有空闲的对象
newParticle = this.particlePool.get();
// console.log("使用空闲对象,尾焰++++++++++++++++", this.particlePool.size());
setParticle(newParticle);
}
else { // 如果没有空闲对象,对象池中备用对象不够时,用 cc.instantiate 重新创建
cc.loader.loadRes("prefabs/Fly/FireParticle", function (err, prefab) {
newParticle = cc.instantiate(prefab);
// console.log("创建新对象,尾焰-------------");
setParticle(newParticle);
});
}
},
5.子弹脚本里,同步粒子发射器的坐标,并且在子弹碰撞死亡后,停止发射并延时回收进管理器
/**子弹尾焰的粒子动画*/
setParticle(particleNode) {
this.particleNode = particleNode;
this.fireParticle = particleNode.getComponent(cc.ParticleSystem);
this.fireParticle.duration = -1;//粒子一直发射
},
//移除当前子弹
removeBullet() {
this.node.removeFromParent();
if (this.bulletManager == null) {
this.bulletManager = FlyBulletMgr.getInstance();
}
if (this.particleNode != null) {
//粒子停止发射
if (this.fireParticle) {
this.fireParticle.stopSystem();
}
//计时器结束后将本子弹/粒子前往管理器回收
this.scheduleOnce(function() {
this.bulletManager.recoveryBullet(this.node, this.nodeID, this.particleNode);
}, 0.2);
this.particleNode = null;
this.fireParticle = null;
}
else {
this.bulletManager.recoveryBullet(this.node, this.nodeID);
}
},
movePos(newPosX, newPosY) {
// console.log(this.bulletID + "=新坐标=>" +cc.winSize.height/2, newPosX + "," + newPosY)
let moveAct = cc.moveTo(this.intervalTime, Math.floor(newPosX), Math.floor(newPosY));
this.node.runAction(moveAct);
//同步粒子的坐标
if (this.particleNode != null) {
let moveAct2 = cc.moveTo(this.intervalTime, Math.floor(newPosX), Math.floor(newPosY));
this.particleNode.runAction(moveAct2);
}
},
这篇博客详细介绍了如何在Unity中创建粒子系统来实现物体如飞机或导弹尾焰的拖尾效果。首先新建粒子并设置其属性,然后将其转化为预制体。在代码中,利用对象池管理粒子,当生成子弹时同时生成粒子,并在子弹移动时同步粒子位置。当子弹碰撞或死亡时,停止粒子发射并延迟回收。这个过程涉及到Unity的粒子系统、节点操作和对象池管理。
3884

被折叠的 条评论
为什么被折叠?



