正确播放Particule系统组件?

时间:2015-12-25 07:26:42

标签: c# unity3d particles particle-system unity5

如何正确播放附加到GameObject的粒子系统组件?我还将以下脚本附加到我的GameObject,但粒子系统不能播放。我该如何解决这个问题?

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

2 个答案:

答案 0 :(得分:2)

假设这是您编写的确切代码,您需要先使用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();
    }
}

答案 1 :(得分:0)

我没有看到你在课堂上宣布距离,但你在更新时使用它。将距离声明为与其他成员的私有浮动,并在开始时定义它。

假设您的代码并非完全相同,那么您的问题似乎也来自使用带距离的固定值。尝试使用小于或等于20.

if(distance <= 20)

或者您可以尝试大于19且小于21。

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