控制粒子运动

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

猜你喜欢

转载自blog.csdn.net/lvcoc/article/details/85091072