加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

【U3D】粒子沿特定路径移动

(2017-04-17 16:36:25)

  U3D中粒子是三维空间中渲染出来的二维图像,通常来说,一个粒子系统是由粒子发射器、动画器和渲染器组成的。我们可以通过脚本来控制粒子系统上的每个粒子,接下来主要讲解一下如何让粒子发射器中产生的粒子沿特定路径进行移动。

      首先呢创建一个粒子发射器,我们打算让其中产生的粒子按照如下(P1--->P2--->P3--->P4--->P5)的方向依次移动,就是下图这样的思路:

       http://img.blog.csdn.net/20160509181121119

     

      假设一下,如果我们通过一个数组来记录每个粒子的位置,让它们依次遵循直线进行移动,这样粒子会排列成一条直线,会产生这样的运动效果:     

http://img.blog.csdn.net/20160509181624511


这种效果不是我想要的,我希望粒子移动能够像这样,它们的方向不变,但是不会排列成一条线,比如这样:


     http://img.blog.csdn.net/20160509182038721

      

      考虑一下,为了实现这个目的, 需要创建一个位置的数组(比如P1到P5的postion),一个表示方向的数组,之后每帧判断粒子的位置,根据当前粒子的位置来决定它的移动方向,当粒子到达任意一点,如果它还在生命周期内,就让它移动到下一个点。

      OK,思路大致如此,打开U3D开始实现效果~~~

      首先,我们需要在Hierarchy面板上点击右键创建一个Particle System(粒子系统),这样系统会自动生成一个发射器,并且默认调用Particle Shader来渲染产生的粒子。

       由于新创建的粒子系统会的Shape自动调用Cone类型,我们需要把它改为Box,这样粒子就会沿着一条直线进行运动:

http://img.blog.csdn.net/20160509183134720?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast


然后在新创建的Particle System上新创建一个脚本TestParticleMovement,代码如下所示:

 

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. public class TestParticleMovement MonoBehaviour  
  5.  
  6.     public List nodes;     
  7.     public Vector3[] directions;  
  8.     private ParticleSystem particles;  
  9.     void Start ()  
  10.      
  11.         GetComponent().startLifetime nodes.Count;  
  12.         if (nodes.Count == 0)  
  13.             Debug.LogError("请添加至少1个node");  
  14.         //自动生成方向  
  15.         directions new Vector3[nodes.Count];  
  16.         for (int 0; nodes.Count; i++)  
  17.             directions[i] (nodes[i] ((i >= 0) nodes[i 1] transform.position));  
  18.      
  19.     void Update ()  
  20.      
  21.         particles GetComponent();  
  22.         ParticleSystem.Particle[] particleList new ParticleSystem.Particle[particles.particleCount];  
  23.         int partCount particles.GetParticles(particleList);  
  24.         for (int 0; partCount; i++)  
  25.          
  26.             // 计算粒子当前的生命  
  27.             float timeALive particleList[i].startLifetime particleList[i].lifetime;  
  28.             float dist GetAddedMagnitude((inttimeALive);  
  29.             int count 0;  
  30.             //判断位置信息  
  31.             while (dist GetAddedMagnitude(count))  
  32.              
  33.                 count++;  
  34.                 particleList[i].velocity directions[count];  
  35.              
  36.          
  37.         particles.SetParticles(particleList, partCount);  
  38.      
  39.     private float GetAddedMagnitude(int count)  
  40.      
  41.         float addedMagnitude 0;  
  42.         for (int 0; count; i++)  
  43.          
  44.             addedMagnitude += directions[i].magnitude;  
  45.          
  46.         return addedMagnitude;  
  47.      
  48.  

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有