c# - 正确播放Particle System组件?

标签 c# particles particle-system unity3d

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

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

最佳答案

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

关于c# - 正确播放Particle System组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460894/

相关文章:

c# - 使用动态表达式 API 时验证 ASP.NET MVC 3 Controller 的操作参数

c# - 在PictureBox中锁定图像文件

iphone - cocos2d游戏,ccparticle系统纹理搞砸了

ios - 让粒子发射器轨迹跟随 spriteKit 中的手指路径

c# - 网格寻呼机无法正常工作

javascript - 与其他软件和程序相比,JavaScript 粒子性能背后的原因

javascript - Particles.js — 框架更改时修改

android-studio - libGDX - 导入到 android studio 的粒子效果工具

swift - 使用粒子系统爆炸 3D 文本(Swift - SceneKit)

c# - 多核服务器上工作进程的 CPU 使用率