Properly play Particule System component?

本小妞迷上赌 提交于 2019-12-13 13:23:44

问题


How can I properly play the particle system component, which is attached to a GameObject? I also attached the following script to my GameObject but the Particle System does not play. How do I fix this?

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

回答1:


I don't see you declaring distance in your class, but you use it under update. Declare distance as a private float with your other members and just define it in start.

Assuming that your code isn't exactly like that, your also issue looks like it stems from using a solid value with distance. Try using less than or equal to 20.

if(distance <= 20)

Or you could try greater than 19 and less than 21.

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




回答2:


Assuming this is the exact code you wrote , You need to first use the GetComponentmethod to be able to perform actions on your particle system

Your code should look like this:

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


来源:https://stackoverflow.com/questions/34460894/properly-play-particule-system-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!