问题
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 GetComponent
method 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