Particles are shaking and distorted when running script

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

public class CloudScript : MonoBehaviour
{
    private float minY = -4f; // The minimum y position of the cloud particles
    private float maxY = 3f; // The maximum y position of the cloud particles

  void Start()
{
    // Get the ParticleSystem component attached to the cloud object
    ParticleSystem particle = GetComponent<ParticleSystem>();
    // Create an array to store the particles in the ParticleSystem
    ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particle.particleCount];
    // Get the particles from the ParticleSystem and store them in the array
    int numParticles = particle.GetParticles(particles);

    // Loop through each particle in the array
    for (int i = 0; i < numParticles; i++) {
        // Get the current position of the particle
        Vector3 newPosition = particles[i].position;
        // Set the y position of the particle to a random value between minY and maxY
        newPosition.y = Random.Range(minY, maxY);
        // Update the position of the particle
        particles[i].position = newPosition;
    }
    particle.SetParticles(particles, numParticles);
}


    // void Update()
    // {
    //     // Get the ParticleSystem component attached to the cloud object
    //     ParticleSystem particle = GetComponent<ParticleSystem>();
    //     // Create an array to store the particles in the ParticleSystem
    //     ParticleSystem.Particle[] particles = new ParticleSystem.Particle[particle.particleCount];
    //     // Get the particles from the ParticleSystem and store them in the array
    //     int numParticles = particle.GetParticles(particles);

    //     // Loop through each particle in the array
    //     for (int i = 0; i < numParticles; i++) {
    //         // Get the current position of the particle
    //         Vector3 newPosition = particles[i].position;
    //         // Set the y position of the particle to a random value between minY and maxY
    //         newPosition.y = Random.Range(minY, maxY);
    //         // Update the position of the particle
    //         particles[i].position = newPosition;
           
    //     }
    //     particle.SetParticles(particles, numParticles);
    // }


   
}

Hello! I’m trying out the particle system, and I want each particle (cloud) to spawn in random y-values, so I came up with this script. When I run it in Update method, the particles shakes and looks distorted. I kinda understand why since each position is set every frame. I tried fixing this by moving the code to the start method, but then they do not spawn at random y-values. What could be the issue here? Here is a video of the shaking happening: Imgur: The magic of the Internet

Having the code in Update will select new Y values every frame. Having the code in Start will run it once when the system is loaded and probably doesn’t contain any particles.

We don’t have “on spawn” event callbacks for each newborn particle, but here is an idea that will work:
Move the code back to update, seed a random number generator with particles.randomSeed, and then generate the Y value. Then each particle will always generate the same value.

It’s not very efficient to run this code every frame on particles whose value has already been set on a previous frame, but at least this gives you a starting point that will work.