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