2

I am trying to have a mini-explosion in my game so I am using a mini-explosion, however it doesn't work, here is a video showing what is happening: https://youtu.be/S_U4AN-RFFQ. Basically it randomly triggers the effect and I am unsure why this happens. Here is the settings for the particle system:

enter image description here

Edit: Here is an image of the emmision module enter image description here

Relevant code:

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

    private void Start()
    {
        if (gameObject.GetComponent<Alk>() != null)
        {
            alk = gameObject.GetComponent<Alk>();
        }
    }
public void ChoseChasingWhatStateToAttackPlayer(NavMeshAgent agent, GameObject playerPos, GameObject Gself, Animator anim, bool MultiAttack, float inRange, float inView, float AttackRange, bool isBlocking, bool HasSeenPLayer)
    {
        switch (state)
        {
            case State.Chase:
                ChasePlayer(playerPos, Gself, anim, agent, AttackRange, inRange, inView, HasSeenPLayer);
                break;
            case State.Attack:
                AttackPlayer(playerPos, anim, agent, Gself, MultiAttack, inRange, inView, AttackRange, isBlocking, HasSeenPLayer);
                break;
            case State.Idle:
                IdlePlayer(playerPos, anim, agent, Gself, inRange, inView, HasSeenPLayer);
                break;
            case State.Wander:
                WanderPlayer(playerPos, anim, agent, Gself, HasSeenPLayer);
                break;
        }
    }

    private float distDif;
    public int random = 0; // random Attacks
    
    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);            
        }

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

        if (random == 3)
        {            
            if (alk != null)
            {
                Debug.Log("This runs");                
                alk.particle.gameObject.SetActive(true);
                alk.particle.Play();                
            }

            StartCoroutine(AlkAnim(anim, "Attack3"));

            //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;

    }

    IEnumerator AlkAnim(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;
        }
    }

    public static bool HasParameter(string paramName, Animator animator) // checks to see if it has that parmameter
    {
        foreach (AnimatorControllerParameter param in animator.parameters)
        {
            if (param.name == paramName)
                return true;
        }
        return false;
    }

    IEnumerator PlayAnim(Animator anim, string booleanName) //sets chosen anim bool true, waits length of the anim, sets bool false
    {
        anim.SetBool(booleanName, true);
        yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length + anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
        anim.SetBool(booleanName, false);        
    }

THis is how it gets called: (In other script)

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());
    }
Arnice123
  • 195
  • 6
  • 1
    Can you show us the Inspector of the Emission module? – derHugo Dec 14 '21 at 18:45
  • 1
    Also how often is `MultAttack` called? – derHugo Dec 14 '21 at 18:47
  • @derHugo 1) if the state is Attack then it will call once every 0.5 seconds. I do not know how to prevent it from being called at the same time. 2) Do you want the emmission module when it is in play mode? – Arnice123 Dec 14 '21 at 18:51
  • 2) I think it shouldn't matter .. but currently we don't see that part in general and maybe something in there is causing the issue. And 1) you could e.g. prevent it from playing if [`alk.particles.isPlaying`](https://docs.unity3d.com/ScriptReference/ParticleSystem-isPlaying.html) is already true, otherwise it will restart it – derHugo Dec 14 '21 at 18:55
  • @derHugo I added the emmision module – Arnice123 Dec 14 '21 at 19:26
  • Are you sure it’s not just playing off screen? – BugFinder Dec 14 '21 at 21:48

0 Answers0