- Home /
How can I delay the cast of a fireball as well as destroy a gameobject mid script
So I have this script launch a fireball on a 3 second cooldown, that works fine. Im trying to implement a cast time by spawning a flame object that lasts 1.5 secs then disapears as the fireball is cast. I keep getting the most retarded error ever so i assume im writing it way wrong...
#pragma strict
public var missileObject:GameObject;
public var throwForce:float = 10;
private var canFire : boolean = true;
public var flameObject:GameObject;
public var fireAnchor:GameObject;
function Start () {
}
function Update () {
if(Input.GetKeyDown("2") && canFire){
var newFlame:GameObject = Instantiate(flameObject,fireAnchor.transform.position, fireAnchor.transform.rotation);
yield WaitForSeconds (1.5)
Destroy newFlame:GameObject;
var newMissile:GameObject = Instantiate(missileObject, this.transform.position, this.transform.rotation);
Physics.IgnoreCollision(newMissile.collider, this.transform.parent.collider);
newMissile.rigidbody.AddForce(throwForce * this.transform.forward, ForceMode.Impulse);
canFire = false;
Reload();
}
}
function Reload (){
yield WaitForSeconds (3);
canFire = true;
}
Answer by whydoidoit · Apr 09, 2013 at 06:01 AM
You need to do:
Destroy(newFlame);
But also you cannot yield inside an Update function - as you see with the Reload - in fact you are better off putting all of the stuff inside the if inside reload now:
function Update () {
if(Input.GetKeyDown("2") && canFire){
FireAndReload();
}
}
function FireAndReload (){
canFire = false;
var newFlame:GameObject = Instantiate(flameObject,fireAnchor.transform.position, fireAnchor.transform.rotation);
yield WaitForSeconds (1.5)
Destroy(newFlame);
var newMissile:GameObject = Instantiate(missileObject, this.transform.position, this.transform.rotation);
Physics.IgnoreCollision(newMissile.collider, this.transform.parent.collider);
newMissile.rigidbody.AddForce(throwForce * this.transform.forward, ForceMode.Impulse);
yield WaitForSeconds (3);
canFire = true;
}
Interesting.. Thank you very much. Guess i'll be spending my uni holidays spicing up my program$$anonymous$$g capabilities.
Answer by ImPvEspec · Apr 23, 2013 at 07:36 AM
This was the final script I ended up using, still a slight issue with the instantiating of the 1st flame effect as it doesnt follow the character, though its only alive for a second so not to bad but rest of it works fine.
#pragma strict
public var missileObject:GameObject;
public var throwForce:float = 10;
private var canFire : boolean = true;
public var flameObject:GameObject;
public var fireAnchor:GameObject;
public var fireballCast:AudioClip;
public var fireballLaunch:AudioClip;
function Start () {
}
function Update () {
if(Input.GetKeyDown("2") && canFire){ //Press 2 key in game to initiate script
FireandReload(); //Calls the below function
}
}
function FireandReload (){
var newFlame:GameObject = Instantiate(flameObject,fireAnchor.transform.position, fireAnchor.transform.rotation); //Instantiates the flame effect on the players hands
audio.PlayOneShot (fireballCast);
yield WaitForSeconds (1);
Destroy (newFlame); //Destroys flame effect
var newMissile:GameObject = Instantiate(missileObject, this.transform.position, this.transform.rotation); //Instantiates the fireball in front of the player
Physics.IgnoreCollision(newMissile.collider, this.transform.parent.collider); //Stops the fireball colliding with the player
newMissile.rigidbody.AddForce(throwForce * this.transform.forward, ForceMode.Impulse); //Propels the fireball forwards
audio.PlayOneShot(fireballLaunch);
canFire = false; //Script can now no longer player again
yield WaitForSeconds (3);
canFire = true; //After 3 second delay script is reset
}