Unity ParticleSystem 控制粒子移动到一个点,沿路径移动.

3 篇文章 0 订阅

一、粒子移动到一个点.

方案1:粒子系统模拟空间为Local.

在这里插入图片描述


	private Transform m_Transform;
    private ParticleSystem m_ParticleSystem;
    private ParticleSystem.Particle[] m_particles;

    public Transform target_Trans;  //目标位置.(手动拖拽)
    private Vector3 pos;            //粒子移动的目标位置.

    void Start()
    {
        m_Transform = gameObject.GetComponent<Transform>();
        m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
        m_particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];  //实例化,个数为粒子系统设置的最大粒子数.

        //ParticleSystem.MainModule main = m_ParticleSystem.main;
        //main.simulationSpace = ParticleSystemSimulationSpace.Custom;    //设置模拟空间为定制.
        //main.customSimulationSpace = target_Trans;                      //定制的模拟空间为目标位置Transform.

        pos = m_Transform.InverseTransformPoint(target_Trans.position); //粒子系统模拟空间设置为Local时,需要把目标位置换成粒子系统的本地坐标.

        //pos = target_Trans.position;  //粒子系统模拟空间为World时,直接把目标位置赋值给pos.
	}

    void Update()
    {
        //获取当前激活的粒子.
        int num = m_ParticleSystem.GetParticles(m_particles);   

        //设置粒子移动.
        for (int i = 0; i < num; i++) 
        {
            //m_particles[i].position = Vector3.Lerp(m_particles[i].position, Vector3.zero, 0.1f);
            m_particles[i].position = Vector3.Lerp(m_particles[i].position, pos, 0.05f);
        }

        //重新赋值粒子.
        m_ParticleSystem.SetParticles(m_particles, num);
    }

方案2:粒子系统模拟空间为World.

在这里插入图片描述


	private Transform m_Transform;
    private ParticleSystem m_ParticleSystem;
    private ParticleSystem.Particle[] m_particles;

    public Transform target_Trans;  //目标位置.(手动拖拽)
    private Vector3 pos;            //粒子移动的目标位置.

    void Start()
    {
        m_Transform = gameObject.GetComponent<Transform>();
        m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
        m_particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];  //实例化,个数为粒子系统设置的最大粒子数.

        //ParticleSystem.MainModule main = m_ParticleSystem.main;
        //main.simulationSpace = ParticleSystemSimulationSpace.Custom;    //设置模拟空间为定制.
        //main.customSimulationSpace = target_Trans;                      //定制的模拟空间为目标位置Transform.

        //pos = m_Transform.InverseTransformPoint(target_Trans.position); //粒子系统模拟空间设置为Local时,需要把目标位置换成粒子系统的本地坐标.

        pos = target_Trans.position;  //粒子系统模拟空间为World时,直接把目标位置赋值给pos.
	}

    void Update()
    {
        //获取当前激活的粒子.
        int num = m_ParticleSystem.GetParticles(m_particles);   

        //设置粒子移动.
        for (int i = 0; i < num; i++) 
        {
            //m_particles[i].position = Vector3.Lerp(m_particles[i].position, Vector3.zero, 0.1f);
            m_particles[i].position = Vector3.Lerp(m_particles[i].position, pos, 0.05f);
        }

        //重新赋值粒子.
        m_ParticleSystem.SetParticles(m_particles, num);
    }

方案3:粒子系统模拟空间为定制.

这里代码设置了粒子系统的定制模拟空间为目标点,设置粒子移动时,直接移动到零点即可.


    private Transform m_Transform;
    private ParticleSystem m_ParticleSystem;
    private ParticleSystem.Particle[] m_particles;

    public Transform target_Trans;  //目标位置.(手动拖拽)
    private Vector3 pos;            //粒子移动的目标位置.

    void Start()
    {
        m_Transform = gameObject.GetComponent<Transform>();
        m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
        m_particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];  //实例化,个数为粒子系统设置的最大粒子数.

        ParticleSystem.MainModule main = m_ParticleSystem.main;
        main.simulationSpace = ParticleSystemSimulationSpace.Custom;    //设置模拟空间为定制.
        main.customSimulationSpace = target_Trans;                      //定制的模拟空间为目标位置Transform.

        //pos = m_Transform.InverseTransformPoint(target_Trans.position); //粒子系统模拟空间设置为Local时,需要把目标位置换成粒子系统的本地坐标.

        //pos = target_Trans.position;  //粒子系统模拟空间为World时,直接把目标位置赋值给pos.
	}

    void Update()
    {
        //获取当前激活的粒子.
        int num = m_ParticleSystem.GetParticles(m_particles);   

        //设置粒子移动.
        for (int i = 0; i < num; i++) 
        {
            m_particles[i].position = Vector3.Lerp(m_particles[i].position, Vector3.zero, 0.1f);
            //m_particles[i].position = Vector3.Lerp(m_particles[i].position, pos, 0.05f);
        }

        //重新赋值粒子.
        m_ParticleSystem.SetParticles(m_particles, num);
    }


二、粒子沿路径移动.

方案1:粒子系统模拟空间为Local.

    private Transform m_Transform;
    private ParticleSystem m_ParticleSystem;
    private ParticleSystem.Particle[] m_Particles;

    [Header("边缘路径点父物体")]
    public Transform targetParent;      //路径点父物体.
    private List<Vector3> targetList;   //路径点集合.
    [Header("粒子移动速度.")]
    [Range(0,8)]
    public float speed = 0.5f;          //粒子移动速度.

	void Start () {
        m_Transform = gameObject.GetComponent<Transform>();
        m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
        m_Particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];

        targetList = new List<Vector3>();
        Transform[] target_TransArray = targetParent.GetComponentsInChildren<Transform>();
        for (int i = 1; i < target_TransArray.Length; i++)
        {
            targetList.Add(target_TransArray[i].position);
        }
	}
	
	void Update () {
        //获取当前激活的粒子.
        int num = m_ParticleSystem.GetParticles(m_Particles);

        //通过设置粒子的持续时间startLifetime和设置移动速度speed,即可使粒子能在时间内走完准确的路径.
        //设置粒子移动.
        for (int i = 0; i < num; i++)
        {
            //计算所用的生命时间与总周期时间的比例.
            float proportion = (m_Particles[i].startLifetime - m_Particles[i].remainingLifetime) / m_Particles[i].startLifetime;

            //根据比例,计算所在路径点的位置.继而设置方向.
            int index = Mathf.FloorToInt(proportion * targetList.Count);
            if (index >= 0 && index < targetList.Count - 1)
            {
                //设置粒子移动的方向.
                //Vector3 direction = targetList[index + 1] - targetList[index];    //模拟空间为World.
                Vector3 direction = m_Transform.InverseTransformPoint(targetList[index + 1]) - m_Transform.InverseTransformPoint(targetList[index]); //模拟空间为Local.
                
                //设置移动的速度.
                m_Particles[i].velocity = direction * (1.0f / speed) * (1.0f / m_Transform.localScale.x);
            }
            else
            {
                //超出路径之后,粒子销毁.
                m_Particles[i].remainingLifetime = 0;
            }
        }

        //重新赋值粒子.
        m_ParticleSystem.SetParticles(m_Particles, num);
	}

方案2:粒子系统模拟空间为World.

    private Transform m_Transform;
    private ParticleSystem m_ParticleSystem;
    private ParticleSystem.Particle[] m_Particles;

    [Header("边缘路径点父物体")]
    public Transform targetParent;      //路径点父物体.
    private List<Vector3> targetList;   //路径点集合.
    [Header("粒子移动速度.")]
    [Range(0,8)]
    public float speed = 0.5f;          //粒子移动速度.

	void Start () {
        m_Transform = gameObject.GetComponent<Transform>();
        m_ParticleSystem = gameObject.GetComponent<ParticleSystem>();
        m_Particles = new ParticleSystem.Particle[m_ParticleSystem.main.maxParticles];

        targetList = new List<Vector3>();
        Transform[] target_TransArray = targetParent.GetComponentsInChildren<Transform>();
        for (int i = 1; i < target_TransArray.Length; i++)
        {
            targetList.Add(target_TransArray[i].position);
        }
	}
	
	void Update () {
        //获取当前激活的粒子.
        int num = m_ParticleSystem.GetParticles(m_Particles);

        //通过设置粒子的持续时间startLifetime和设置移动速度speed,即可使粒子能在时间内走完准确的路径.
        //设置粒子移动.
        for (int i = 0; i < num; i++)
        {
            //计算所用的生命时间与总周期时间的比例.
            float proportion = (m_Particles[i].startLifetime - m_Particles[i].remainingLifetime) / m_Particles[i].startLifetime;

            //根据比例,计算所在路径点的位置.继而设置方向.
            int index = Mathf.FloorToInt(proportion * targetList.Count);
            if (index >= 0 && index < targetList.Count - 1)
            {
                //设置粒子移动的方向.
                Vector3 direction = targetList[index + 1] - targetList[index];    //模拟空间为World.
                //Vector3 direction = m_Transform.InverseTransformPoint(targetList[index + 1]) - m_Transform.InverseTransformPoint(targetList[index]); //模拟空间为Local.
                
                //设置移动的速度.
                //m_Particles[i].velocity = direction * (1.0f / speed) * (1.0f / m_Transform.localScale.x);
                m_Particles[i].velocity = direction * (1.0f / speed);
            }
            else
            {
                //超出路径之后,粒子销毁.
                m_Particles[i].remainingLifetime = 0;
            }
        }

        //重新赋值粒子.
        m_ParticleSystem.SetParticles(m_Particles, num);
	}

参考链接:
粒子移动
粒子移动
粒子移动

  • 11
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值