控制粒子运动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParticleGrid : MonoBehaviour {

    ParticleSystem particleSystem;

    public Vector3 bounds = new Vector3(25.0f, 25.0f, 25.0f);//矩形界限
    public Vector3 resolution = new Vector3(1, 100, 200);//分辨率
    private ParticleSystem.Particle[] particles;
    private void Update()
    {
        for (int i = 0; i < ij; i++)
        {

            Vector3 pos = particles[i].position;
            pos.y = Mathf.Sin(Mathf.PI*(pos.z + Time.time));
            //pos.y += Mathf.Sin(2f * Mathf.PI * (pos.z + 2f * Time.time)) / 2f;
            //pos.y *= 2f / 2f;
            particles[i].position = pos;
        }
        // Apply the particle changes to the particle system
        particleSystem.SetParticles(particles, ij);
    }
    ParticleSystem.EmitParams ep;
    private int ij;
    void Awake()
    {
        particleSystem = GetComponent<ParticleSystem>();
        this.particles = new ParticleSystem.Particle[this.particleSystem.main.maxParticles];
        Vector3 scale;
        Vector3 boundsHalf = bounds / 2.0f;

        //每个粒子占的区域大小
        scale.x = bounds.x / resolution.x;
        scale.y = bounds.y / resolution.y;
        scale.z = bounds.z / resolution.z;
        //覆盖粒子属性
         ep = new ParticleSystem.EmitParams();
        //遍历所有粒子
        for (int i = 0; i < resolution.x; i++)
        {
            for (int j = 0; j < resolution.y; j++)
            {
                for (int k = 0; k < resolution.z; k++)
                {
                    Vector3 position;

                    position.x = (i * scale.x) - boundsHalf.x;
                    position.y = (j * scale.y) - boundsHalf.y;
                    position.z = (k * scale.z) - boundsHalf.z;

                    ep.position = position;
                  particleSystem.Emit(ep, 1);
                }
            }
        }
        particles = new ParticleSystem.Particle[particleSystem.main.maxParticles];
        ij = particleSystem.GetParticles(particles);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值