C# 正确使用微粒系统组件?

C# 正确使用微粒系统组件?,c#,unity3d,particles,particle-system,unity5,C#,Unity3d,Particles,Particle System,Unity5,如何正确地播放附加到游戏对象的粒子系统组件?我还将以下脚本附加到我的游戏对象上,但粒子系统不起作用。我该如何解决这个问题 public Transform gameobject1; public Transform gameobject2; public ParticleSystem particules; void Start() { float distance = Vector3.Distance(gameobject1.position, gameobject2.positio

如何正确地播放附加到游戏对象的粒子系统组件?我还将以下脚本附加到我的游戏对象上,但粒子系统不起作用。我该如何解决这个问题

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

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

假设您的代码与此不完全相同,那么您的问题似乎也源于使用带距离的实数值。尝试使用小于或等于20的值


if(distance假设这是您编写的确切代码,您需要首先使用
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();
    }
}
public-Transform-gameobject1;
公共物品2;
公共粒子系统粒子;
公众浮标距离;
//我们在开始函数中抓取粒子系统
void Start()
{
particules=GetComponent();
}
无效更新()
{
//你必须不断检查距离
//如果希望粒子系统播放距离小于20的瞬间
//所以我们在更新函数中设置了距离变量。
距离=矢量3.距离(gameobject1.position,gameobject2.position);
//如果对象彼此越来越远,请使用(距离>=20)

如果(距离@Micky)不是我的问题。我编辑了这个问题。啊,是的。谢谢Dimitri@Micky我更详细地编辑了这个问题。