如何获取粒子系统之一?

3
我有一个敌人预制体(ZomBunny),其中包含两个部分系统,当敌人被攻击和死亡时应该播放。我遇到的问题是,使用下面的代码时,我只能访问其中一个粒子系统(HitParticle)。 层次结构 enter image description here EnemyHealth
public class EnemyHealth : MonoBehaviour
{
    public int startingHealth = 100;          // The amount of health the enemy starts the game with.
    public int currentHealth;                    // The current health the enemy has.
    public float sinkSpeed = 2.5f;           // The speed at which the enemy sinks through the floor when dead.
    public int scoreValue = 10;               // The amount added to the player's score when the enemy dies.
    public AudioClip deathClip;              // The sound to play when the enemy dies.
    public GameObject text;

    Animator anim;                              // Reference to the animator.
    AudioSource enemyAudio;              // Reference to the audio source.
    ParticleSystem hitParticles;             // Reference to the particle system that plays when the enemy is damaged.
    ParticleSystem deathParticles;
    CapsuleCollider capsuleCollider;     // Reference to the capsule collider.
    bool isDead;                                  // Whether the enemy is dead.
    bool isSinking;                               // Whether the enemy has started sinking through the floor.

    void Awake ()
    {
        // Setting up the references.
        anim = GetComponent <Animator> ();
        enemyAudio = GetComponent <AudioSource> ();
        hitParticles = GetComponentInChildren <ParticleSystem> ();
        deathParticles = GetComponentInChildren <ParticleSystem> ();
        capsuleCollider = GetComponent <CapsuleCollider> ();

        // Setting the current health when the enemy first spawns.
        currentHealth = startingHealth;
    }

    void Update ()
    {
        // If the enemy should be sinking...
        if(isSinking)
        {
            // ... move the enemy down by the sinkSpeed per second.
            transform.Translate (-Vector3.up * sinkSpeed * Time.deltaTime);
        }
    }

    public void TakeDamage (int amount, Vector3 hitPoint)
    {
        // If the enemy is dead...
        if(isDead)
            // ... no need to take damage so exit the function.
            return;

        // Play the hurt sound effect.
        enemyAudio.Play ();

        // Reduce the current health by the amount of damage sustained.
        currentHealth -= amount;

        // Set the position of the particle system to where the hit was sustained.
        hitParticles.transform.position = hitPoint;

        // And play the particles.
        hitParticles.Play();

        // If the current health is less than or equal to zero...
        if(currentHealth <= 0)
        {
            // ... the enemy is dead.
            Death ();
        }
    }

    void Death ()
    {
        // The enemy is dead.
        isDead = true;

        // Turn the collider into a trigger so shots can pass through it.
        capsuleCollider.isTrigger = true;

        // Tell the animator that the enemy is dead.
        anim.SetTrigger ("Dead");

        deathParticles.Play();
        // Change the audio clip of the audio source to the death clip and play it (this will stop the hurt clip playing).
        enemyAudio.clip = deathClip;
        enemyAudio.Play ();
    }

    public void StartSinking ()
    {

        // Find and disable the Nav Mesh Agent.
        GetComponent <UnityEngine.AI.NavMeshAgent> ().enabled = false;

        // Find the rigidbody component and make it kinematic (since we use Translate to sink the enemy).
        GetComponent <Rigidbody> ().isKinematic = true;

        // The enemy should no sink.
        isSinking = true;

        // Increase the score by the enemy's score value.
        ScoreManager.score += scoreValue;

        // After 2 seconds destory the enemy.
        Destroy (gameObject, 2f);
    }
}

你能上传一张层级结构的图片,展示粒子的位置吗? - Programmer
更新了层级结构的照片。 - Ved Sarkar
1个回答

10
当你执行以下操作时:
hitParticles = GetComponentInChildren<ParticleSystem>();
deathParticles = GetComponentInChildren<ParticleSystem>();

Unity将获取子级上第一个ParticleSystem组件。您需要一种方法来区分您想要的组件。有许多方法可以实现这一点。

如果EnemyHealth脚本附加到ZomBunny游戏对象:

方法1:

ZomBunny游戏对象下,hitParticles是子对象0Zombunny是子对象1deathParticles是子对象2

应该是:

hitParticles = transform.GetChild(0).GetComponentInChildren<ParticleSystem>();
deathParticles = transform.GetChild(2).GetComponentInChildren<ParticleSystem>();

方法2:

找到两个游戏对象,然后从每个游戏对象中获取组件:

hitParticles = GameObject.Find("HitParticles").GetComponent<ParticleSystem>();
deathParticles = GameObject.Find("DeathParticles").GetComponent<ParticleSystem>();

方法三:

使用返回数组的GetComponentsInChildren而不是GetComponentInChildren。由于顺序是不可预测的,因此我们还必须检查GameObject的名称以确保它是正确的ParticleSystem

ParticleSystem[] ps = GetComponentsInChildren<ParticleSystem>();
for (int i = 0; i < ps.Length; i++)
{
    if (ps[i].gameObject.name == "HitParticles")
    {
        hitParticles = ps[i];
    }
    else if (ps[i].gameObject.name == "DeathParticles")
    {
        deathParticles = ps[i];
    }
}

您似乎是新手使用Unity。了解在这种情况下可以使用的许多选项会更好。


尝试了方法1和2,第一种方法按预期工作,而第二种方法虽然可以正常工作,但会出现MissingReferenceException:对象类型为'ParticleSystem'已被销毁,但您仍在尝试访问它。您的脚本应该检查它是否为空或者您不应该销毁该对象。错误。 - Ved Sarkar
无法确定为什么会发生这种情况。使用适合您的那个。 - Programmer
非常感谢您的帮助。 - Ved Sarkar
第二种方法应该是 GetComponent 而不是 GetComponentInChildren。我回答时有点困。已经修复了,不客气! - Programmer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接