Enemy AI Particle System Instantiation after a condition is met

Hi… I am trying to make a runner game… I added some enemies(in this case, dragons) who appear at random distance from the player along z axis…my characters are 3d but the game looks like 2d…
I want my dragons to start instantiating “Fire” particle system from their mouth but I do not know where to put the code…I’m learning from youtube tutorials and I thought i would add something extra and cool.
The fire keeps instantiating so fast that my unity freezes and I have to force restart my pc.
Below is the code.

using UnityEngine;
using System.Collections;

public class DragonDamage : MonoBehaviour {

private int damageTaken;
public GameObject ps;
public GameObject player;
public Rigidbody fire;
public Transform firePosition;
//private int fireTimes=2;
private float distance;

// Use this for initialization
void Start () {

}

// Update is called once per frame
private void Update () {

//if the distance between the player and the dragon is less than any number then dragon instantiates fire

distance=player.gameObject.transform.position.z-this.transform.position.z;

if(distance<30.0f)
{

BreatheFire();
return;

}

// i think this is where the problem starts…but i don’t know how to fix it…

}

void OnCollisionEnter(Collision col)
{
if(col.gameObject.tag==“Bullet”)
{
damageTaken++;
DragonDead();
}

}

void BreatheFire()
{

Rigidbody rocketInstance;
rocketInstance=Instantiate(fire, firePosition.position, firePosition.rotation)as Rigidbody;

rocketInstance.AddForce(firePosition.forward*300.0f);
Destroy(rocketInstance,3.0f);

}

void DragonDead()
{
//If the bullet hits the dragon more than 3 times, dragon explodes
if(damageTaken>3)
{
Instantiate(ps,transform.position,Quaternion.identity);
Destroy(gameObject);
}
}
}

Also, the script does not seem to work with other 3 dragons that i have…only one dragon behaves in such way

How can i make the dragons instantiate after random time?..i used the script to randomly appear between some distance

Once again,I’m a beginner and I wrote these scripts from forums and tutorials… please don’t mind if the problem is a simple one…thank you very much

Right now you’re instantiating your fire every frame. You could try to lock the method for some seconds, after instantiating the fire, by using coroutines.

This should probably work:

bool fireLocked = false;

void Update()
{
    if(distance<30.0f && !fireLocked)
    {
        BreatheFire();
        StartCoroutine(LockFire());
    }
}

IEnumerator LockFire()
{
    fireLocked = true;
    yield return new WaitForSeconds(2);
    fireLocked = false;
}

(There might be some syntax errors. I had no chance to check the code)

1 Like

thank you very much…this fixed it …one another problem has arisen though…
the fire that the dragon breathes destroys after some seconds but it still stays in the hierrarchy and in the game scene, it is visible with those “white starry things” which come up after the particle effect is finished playing…
shouldn’t this code fix that? Destroy(rocketInstance,5.0f);
“rocketinstance” i forgot to change that(from unity official instantiate tutorial)…

Mesh colliders didn’t seem to work on those dragons…I have a mesh that came with the dragon pack from unity store… i have reduced the dragon scale but the mesh scale remains the same…anyway to change that?
Thankyou

Edit: Fixed the explosion but not the mesh collider

It seems that you’re destroying the Rigidbody component instead of the GameObject.

Try this:

Destroy(rocketInstance.gameObject,3.0f);

And I would advise against using a mesh collider. Usually the complexity of a mesh collider is not needed. Try using a box collider. It might already solve your problem.

Yeah…i solved the problem…Thank you very much