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