【问题标题】:How to enable a particle system?如何启用粒子系统?
【发布时间】:2022-01-15 09:56:28
【问题描述】:

当攻击动画发生时,我试图让粒子系统发生爆炸效果。问题是它不起作用(不播放粒子系统,只是停用)。此外,这是一个适用于我游戏中所有敌人的脚本,而不仅仅是这个,这就是为什么我有一个单独的脚本来抓取粒子系统。如果需要任何代码,我可以添加它。

编辑:我可以编辑动画,但我不确定这是否有用。

系统设置如下:

这里是所有相关的sn-ps代码:

public class Alk : MonoBehaviour
{
    public ParticleSystem particle;
}
    Alk alk;

    private void Start()
    {
        if (gameObject.GetComponent<Alk>() != null)
        {
            alk = gameObject.GetComponent<Alk>();
        }
    }


public void AttackPlayer(GameObject playerPos, Animator anim, NavMeshAgent agent, GameObject self, bool MultiAttack, float inRange, float inView, float AttackRange, bool isBlocking, bool HasSeenPLayer)
    {
        distDif = Vector3.Distance(self.transform.position, playerPos.transform.position);

        if (distDif >= AttackRange)
        {
            agent.SetDestination(playerPos.transform.position);
            //anim.SetBool("walk", false);
        }

        else if (distDif <= AttackRange)
        {
            anim.SetBool("walk", false);
        }

        if (MultiAttack)
        {
            MultAttack(playerPos, anim, agent, self, inRange, inView);            
        }
        else
        {
            anim.SetBool("Attack2", false);
            anim.SetBool("Attack3", false);
            StartCoroutine(PlayAnim(anim, "Attack"));
        }
        
        if (!PlayerPos.FindPlayerPos(AttackRange, playerPos.transform, inView, self, HasSeenPLayer))
        {
            anim.SetBool("Attack", false);
            anim.SetBool("Attack2", false);
            anim.SetBool("Attack3", false);
            state = State.Chase;
        }
    }

 public void MultAttack(GameObject playerPos, Animator anim, NavMeshAgent agent, GameObject self, float inRange, float inView)
    {      
        if (random == 0) random = Random.Range(1, 5);

        if (random == 1)
        {
            StartCoroutine(PlayAnim(anim, "Attack"));
            random = 0;            
            return;
        }

        if (random == 2)
        {
            if (HasParameter("Attack2", anim))
            {
                StartCoroutine(PlayAnim(anim, "Attack2"));
                random = 0;
                return;
            }

            StartCoroutine(PlayAnim(anim, "Attack"));
            random = 0;
            return;
        }
        // important code
        if (random == 3) //----------------------------------------
        {
            if (alk != null)
            {
                Debug.Log("This runs"); // does run 
                
                alk.particle.gameObject.SetActive(true);
                alk.particle.Play();
                StartCoroutine(PlayAnim(anim, "Attack3"));
                alk.particle.Stop();
                alk.particle.gameObject.SetActive(false);
                random = 0;
                return;
            }

            if (HasParameter("Attack3", anim))
            {
                StartCoroutine(PlayAnim(anim, "Attack3"));
                random = 0;
                return;
            }

            StartCoroutine(PlayAnim(anim, "Attack"));
            random = 0;
            return;
        }

        if (random == 4)
        {
            BlockPlayer(playerPos, anim, agent, inRange, inView, self);
            random = 0;
            return;
        }

        StartCoroutine(PlayAnim(anim, "Attack"));
        random = 0;
        return;

    }

    public static bool HasParameter(string paramName, Animator animator)
    {
        foreach (AnimatorControllerParameter param in animator.parameters)
        {
            if (param.name == paramName)
                return true;
        }
        return false;
    }
    IEnumerator PlayAnim(Animator anim, string booleanName)
    {
        anim.SetBool(booleanName, true);
        yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length + anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
        anim.SetBool(booleanName, false);        
    }

它是如何被调用的(这是在另一个脚本中):

void Start()
    {      

        StartCoroutine(GetGood());
    }

    IEnumerator GetGood()
    {        
        TtF.ChoseChasingWhatStateToAttackPlayer(agent, Player_pos.player, self, anim, MultiAttack, inRange, inView, AttackRange, isBlocking, hasSeenPlayer);
        
        yield return new WaitForSeconds(0.5f);
        StartCoroutine(GetGood());
    }

【问题讨论】:

    标签: c# unity3d particle-system


    【解决方案1】:

    我发现了问题:

    alk.particle.gameObject.SetActive(true); // Activates the system
    alk.particle.Play(); // Plays the particle
    StartCoroutine(PlayAnim(anim, "Attack3")); // Starts Coroutine, and while it is waiting, runs the rest of the code
    alk.particle.Stop(); // Immediately stops the particle
    alk.particle.gameObject.SetActive(false); // and deactivates the system
    

    解决方案是将代码放入协程中,如下所示:

    IEnumerator PlayAnim(Animator anim, string booleanName)
    {
        anim.SetBool(booleanName, true);
        yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length + anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
        anim.SetBool(booleanName, false);
        if (alk != null)
        {
            alk.particle.Stop();
            alk.particle.gameObject.SetActive(false);
            random = 0;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-29
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多