Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Particle count during Read and Write is always at max Count

Discussion in 'General Graphics' started by Spikebor, Nov 8, 2023.

  1. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    278
    I've sent this bug report, if anyone have this resolved can shed me some light, thanks!
    Unity 2022.3.11f1 Windows Editor
    1. What happened
    -Particle count during Read and Write is always at max Count

    2. Expected result: particle still spawn normally using its spawning rate

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. //this code will lerp the particle velocity to moving toward target
    5. //can test in edit mode.
    6. //select the particle system > lock its inspector > now select and move the cube
    7. //the particles will follow the cube.
    8. //the problem is particle count during read and write is wrong.
    9. //expected result: particle still spawn normally using its spawning rate
    10. //actual result: particle constantly have a alive particle count = max count.
    11.  
    12. [RequireComponent(typeof(ParticleSystem)), ExecuteAlways]
    13. public class TestParticleReadAndWrite : MonoBehaviour
    14. {
    15.     [SerializeField] new ParticleSystem particleSystem;
    16.     [SerializeField] Transform m_target;
    17.     [SerializeField] float m_speed = 2f, _lerpSpd = 10f;
    18.  
    19.     ParticleSystem.Particle[] particles;
    20.  
    21.     private void OnValidate()
    22.     {
    23.         if (particles == null || (particles != null && particles.Length != particleSystem.main.maxParticles))
    24.         {
    25.             particles = new ParticleSystem.Particle[particleSystem.main.maxParticles];
    26.         }
    27.     }
    28.  
    29.     private void Reset()
    30.     {
    31.         particleSystem = GetComponent<ParticleSystem>();
    32.     }
    33.  
    34.     private void LateUpdate()
    35.     {
    36.         if (particleSystem.isPlaying == false) return;
    37.  
    38.         //read count
    39.         var aliveCount = particleSystem.GetParticles(particles);
    40.         //change velocity to move toward target
    41.         for (int i = 0; i < aliveCount; i++)
    42.         {
    43.             var targetPos = m_target.position;
    44.             var targetVelo = (targetPos - particles[i].position).normalized * m_speed;
    45.             //changing velocity will not have effect if the module velocity over life time still active
    46.             particles[i].velocity = Vector3.Lerp(particles[i].velocity,targetVelo, _lerpSpd);
    47.         }
    48.         //write the particle back          
    49.         particleSystem.SetParticles(particles);
    50.         //expected result: particle still spawn normally using its spawning rate
    51.         //actual result: particle constantly have a alive particle count = max count.
    52.     }
    53. }