blinking particles

Hi
I created a code that with mouse movement i can change the velocity of particles but i have a problem, I cant understand why my particles sometime blink.
the particles once a few second disappear for a second and then reappear . the particles continue with their path because of that i don’t think they died.

I need ideas what could happen.

BP

EDIT: hmmm i thought i posted the code . here it is

using UnityEngine;

public class SunPhotonsExperiments : MonoBehaviour 
{
    public Vector3 NorthPole = new Vector3(-10,5f,0);//The Location the photons should move
    public Vector3 Speed = new Vector3 (-4f,1f,0f);// The speed at which the photons start to move
    public float AccelDampen = 0.04f; //The value we multiply the speed change if it far away
    public float CloseToPoleDampen = 0.4f;////The value we multiply the speed change if it close

    ParticleSystem.Particle[] Photons2;//Our Particles
    int MAX_NUMBER_OF_PARTICLES = 500; //For the for loop and set.

    float StartTime;//for future uses

    float Mindistance = 0.2f;// The distance from which we consider the object arrived
    float MaxForce = 5;//Max allowed acceleration
    bool StartMovingToPole = false;//Should the particles start with the speed
    float MaxAllowedSpeed = 10f; // lowest speed which allowed for particles

    bool SpeedChanged = false;

    void Start() 
    {
        StartTime = Time.time;
        Photons2 = new ParticleSystem.Particle[MAX_NUMBER_OF_PARTICLES];
    }


    void FixedUpdate()
    {
        GameObject.Find("Sun").GetComponent<ParticleSystem>().GetParticles(Photons2);//Inits the array with current particles
        //ParticleSystem ps=   GameObject.Find("Sun").GetComponent<ParticleSystem>(); //For Debugging. Delete It
        int i;// for For loop
        if (GameObject.Find("Main Camera").GetComponent<MouseMovement>().IsMouseDraged) //
        {
            StartMovingToPole = true;//StartMoving To the Pole
            if(!SpeedChanged )
            { 
                Speed = GameObject.Find("Main Camera").GetComponent<MouseMovement>().MouseDragSpeed*0.08f;
                SpeedChanged = true;
            }
        }
        if (StartMovingToPole)
        {
            for ( i = 0; i < MAX_NUMBER_OF_PARTICLES; i++)
            {
                Vector3 Force = NorthPole - Photons2*.position;//also distance*

if (Force.magnitude > 20)// if it is very far away (like at the beggining of the particle life)
{
Photons2*.color = new Color(10,8,0);//yellow collor for fun.*
Photons2*.velocity = Speed;//make the photons velocity equal to the predetermined speed*

if (i == MAX_NUMBER_OF_PARTICLES-1) //initialized
SpeedChanged = false;

}
else
{
float DampenValue;//dummy for storing the dampen value
if (Force.magnitude>10) //medium distance. low dampening
DampenValue = AccelDampen;
else
DampenValue = CloseToPoleDampen;//close distance.high dampen

if (Force.magnitude < Mindistance) //if the photon close to the location
{
Photons2*.velocity = new Vector3 (0f,0f,0f);//*
Photons2*.color = new Color(0,0,0,0) ;//make them invisible*
}
if (Force.magnitude > MaxForce) //if the force(accel) is too high lower it to max predetermined value
{
Force.Normalize();
Force*=MaxForce;
}
this.Photons2.velocity = this.Photons2_.velocity+DampenValue * Force;//calculate new velocity_
if( this.Photons2*.velocity.magnitude > MaxAllowedSpeed )*
this.Photons2.velocity = this.Photons2_.velocity.normalized * MaxAllowedSpeed;//if after all this the speed is too high. lower it_
}
}

particleSystem.SetParticles(Photons2,MAX_NUMBER_OF_PARTICLES);
}
}
}

I’m not really sure why they are blinking. Is it their speed that gets them suddenly out of view ? Do they get the color (0,0,0,0) when they shouldn’t ?

Anyway, I can give you some optimization advices.

  • Don’t call Find() that often. Do it once and store the value.
  • Vector3.magnitude isn’t a value stored somewhere, it’s a property that perform a calculation, you should store it at the beginning of the loop. Furthermore, for distance comparison, here is a quick math demonstration :

vec.magnitude < minDist;

<=> vec.magnitude * vec.magnitude < minDist * minDist;

<=> vec.sqrMangitude < minDist * minDist;

Store the sqrMag and compare it to 400 (20*20), 100 or minDistance * minDistance.