Is the Particule System component playing properly?

How to correctly reproduce a particle system component that is attached to a GameObject? I also added the following script to my GameObject, but the Particle System does not play. How to fix it?

public Transform gameobject1;
public Transform gameobject2;
public ParticleSystem particules;

void Start()
{
    float distance = Vector3.Distance(gameobject1.position, gameobject2.position);
}

void Update()
{
    if(distance == 20)
  {
      particules.Play();
  }
}
+4
source share
2 answers

I don’t see you declaring a distance in your class, but use it under the update. Declare the distance as a private float with other members and simply define it at the beginning.

Assuming your code is not exactly like that, your problem also looks due to using a solid value with distance. Try using less than or equal to 20.

if(distance <= 20)

19 21.

if(distance <= 21 && distance >= 19)

0

, , , GetComponent,

:

public Transform gameobject1;
public Transform gameobject2;
public ParticleSystem particules;
public float distance;

//We grab the particle system in the start function
void Start()
{
    particules = GetComponent<ParticleSystem>();
}

void Update()
{
    //You have to keep checking for the Distance
    //if you want the particle system to play the moment distance goes below 20 
    //so we set our distance variable in the Update function.
    distance = Vector3.Distance(gameobject1.position, gameobject2.position);

    //if the objects are getting far from each other , use (distance >= 20)
    if(distance <= 20) 
    {
        particules.Play();
    }
}
+2

Source: https://habr.com/ru/post/1621689/


All Articles