- Home /
Fire time interval problem?
Hello everyone. I have a problem. In my 2d maze game i created four bomb machine. These machines should fire bomps (Sphere) fixed time interval. (but each one is different time. Ex: first machine each two sec., second is 1sec etc.). I can do it for one sphere. But when i put sphere each machine and set fire rate and dead time on the inspector, all fire times start to run wrong and badly. This is my code:
var projectile : Transform;
var bulletSpeed : float = 20;
var destroy: float =5.0;
private var nextFireTime : float = 0.0;
private var fireRate : float = 1.0;
function Update () {
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
while(nextFireTime < Time.time){
fireProjectile();
WaitAndDestroy();
nextFireTime = Time.time + fireRate;
}
}
function fireProjectile(){
var clone : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.rigidbody.AddForce(clone.transform.forward * bulletSpeed);
}
function WaitAndDestroy()
{
yield WaitForSeconds(destroy);
Destroy(GameObject.Find("Roll A Ball(Clone)"));
}
Also i put spheres projectile' roll a ball. I tried to create four different script four different roll a ball for sphere. But again i can not do it. But code run for one fire correctly. Please help me about this problem.
Answer by Zarenityx · Jan 03, 2013 at 08:47 PM
It is glitching probably because you are using GameObject.find(), which works fine when there is only one object of that name, but can cause problems when there are more than one. If you destroy the clone variable, instead of the return of the Find() function, it should work fine, because clone is specific to the script, whereas there may be multiple "Roll A Ball(Clone)" instances. And the yield is unnecessary, as the time can simply be added as a second parameter for Destroy(). Also,a lot of games wait until the projectile is destroyed for bomb-like projectiles,rather than a fixed firerate, because it is easier on performance and will make sure there are only as many bombs as there are launchers at one time or another, so there is no chance of overpopulating the scene with bombs and possibly crashing the game with an access violation error. I would have created it this way:
/* Add a smoothlookat component to your launcher and set it to target the player, then add this javascript to it. */
#pragma strict
var projectile : GameObject;
var initForce : float;
private var current : GameObject;
function Update(){
if(current == null){
current=Instantiate(projectile,transform.position,transform.rotation);
current.rigidbody.velocity = Vector3(0,0,initForce);
}
}
And
//Put this on the bullet, as well as a rigidbody.
var MinLife : float;
var MaxLife : float;
var RemainTime : float;
var Explosion : GameObject;
private var lifetime : float;
function Awake(){
lifetime = Random.Range(MinLife,MaxLife);
StartDeath();
}
function StartDeath(){
yield WaitForSeconds(lifetime);
Instantiate(Explosion,transform.position,transform.rotation);
Destroy(gameObject,RemainTime);
Note that you will need multiple bullet types for my method, but you may find it faster and more efficient.
Your welcome. Did you end up using the manual and constant firerate, or the automatic firerate? Just wondering.