如何获得一种粒子系统? [英] How to get one of the particle system?

查看:117
本文介绍了如何获得一种粒子系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个敌人的预制件(ZomBunny),这是两个部分系统,当敌人被击中和死时,它们将被播放.我面临的问题是,我只能访问其中一个粒子使用下面的向下代码时系统(HitParticle).

I have an enemy prefab(ZomBunny) in it their are two partial systems which are to be played when the enemy is hit and when the enemy die.The problem i am facing is ,i can only access only one of the particle system(HitParticle) when using the down below code.

层次结构

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

推荐答案

当您这样做:

hitParticles = GetComponentInChildren<ParticleSystem>();
deathParticles = GetComponentInChildren<ParticleSystem>();

Unity将在子级上获得第一个ParticleSystem组件.您需要一种方法来区分您想要的一个.有很多方法可以做到这一点.

Unity will get the first ParticleSystem component on the child. You need a way to distinguish which one you want. There are many ways to do this.

如果EnemyHealth脚本已附加到 ZomBunny GameObject:

If the EnemyHealth script is attached to the ZomBunny GameObject:

方法1 :

ZomBunny GameObject中, hitParticles 是子 0 . Zombunny 是子 1 ,而 deathParticles 是子 2 .

Under the ZomBunny GameObject, hitParticles is child 0. Zombunny is child 1 and deathParticles is child 2.

应该是:

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

方法2 :

找到两个GameObject,然后从它们每个中获取组件:

Find both GameObjects then get the components from each of them:

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

方法3 :

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

Use GetComponentsInChildren which returns array instead of GetComponentInChildren. The order is unpredictable so we must also check the name of the GameObject to make sure that it is the correct 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的新手.最好了解这种情况下的许多选项.

You seem to be new to Unity. It's better to know many options you have in this case.

这篇关于如何获得一种粒子系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆