scripting a trigger to emit a flame on my character then..

Hello fellow game makers. I am rather new to Unity but have fundamental knowledge of the program and the basics on reading the script part in Unityscript. and some on c#

My question is i made this script so my wizard character can charge a fireball, then once the button is released he shoots the fireball.
there is two problems i can’t seem to figure out.

#1: how to trigger the flames particles on his hands to emit once button is held down, and then they stop emitting once the button is let go.

#2: he shoots the fireball after i let the button go, but i want him to charge the fireball in order to shoot it out while holding the button down, not shoot it every time the key is instantly pushed.
anyways heres the script, it was back engineered some what to the concept of the Tornado twins fireball script.

var fireballPrefab : Transform;
var emit : boolean;

//spell casts variables here
var cast1Animation : AnimationClip;

var cast1AnimationSpeed : float = 3.0;

function Update () {

   // Casting animation from casting to idle     
   if (Input.GetKey ("1"))
        animation.CrossFade ("cast1");
   else
        animation.CrossFade ("idle");
        
   // Fireball Relased after fire button is release.
 if (Input.GetButtonUp("Fire1")) { 
        
         var bullet = Instantiate(fireballPrefab,
         GameObject.Find("fireballSpawn").transform.position,
         Quaternion.identity);       
         bullet.rigidbody.AddForce(transform.forward * 1000);
 }

}
function Start () {

         *******this part here dont work i get a message saying character********* 
         *******doesn't have ParticleMitter attched. which he does. its placed****
         *******on him as a child*************************************************

         // Emit Particles for 1 second
         particleEmitter.emit = true;
         yield WaitForSeconds(1);
         //Then Stop
         particleEmitter.emit = false;

}

any ideas or help will be great. thanks!

Hey, welcome to UnityAnswers, I’ve found this place an immense resource and Get-Out-Of-Jail-Free card when you really get stuck.

What you need to know from the looks of it is that there are 3 different Input methods for buttons: Up, Down, and Stay (not actually called Stay, it’s nameless). GetButtonUp runs code once when the button is released, GetButtonDown runs code once when the button is first pressed, GetButton runs code continuously while the button is held. You can have a read all about the various Input types on the Script Reference website here.

So for your case, what you’ll want to do is:

  1. GetButtonDown: Spawn the fireball, and enable the particle emitter on his hands
  2. GetButton: Make it grow bigger for as long as this is running
  3. GetButtonUp: Launch the fireball, and disable the particle emitter on his hands

Now to get the particle emitter working, what you have to understand is that if you say particleEmitter.emit = true; then it only looks for an emitter on the object that this script is attached to. What you’ll want to do is tell it to do this on a different object, like this:

GameObject.Find("EMITTER NAME GOES HERE").particleEmitter.emit = true;

But obviously replacing “EMITTER NAME GOES HERE” with the name of the object that has the emitter on it.

Hope all this helps!

Thank you so much for that!
You were right about the GameObject.Find part. as soon as I input the name of my particle prefab in there the particle started to emit.
so now as the button is pressed the particle begins to materialize, as the button is let go the particle dissipates.
I went back and redid the script (As shown Bellow)
now to integrate the “GetButton” and another prefab with more emission to give the look as the fire is growing.
I’ll post the end result script once i get it working how I want.
but for now the principle is there and I no longer need to crack my head open thanks to you sir.

Again thank you sir, your a life saver!!!

function Update ()

{

 if (Input.GetButtonDown("Fire1"))

   {

   GameObject.Find("fireballTestEmit").particleEmitter.emit = true;

   }

 if(Input.GetButtonUp("Fire1"))

   {

    GameObject.Find("fireballTestEmit").particleEmitter.emit = false;

   }

}

function Start()

{

GameObject.Find(“fireballTestEmit”).particleEmitter.emit = false;

}

well that did work.
now i have a new problem.
I put the particle prefabs on each of his hands. when i hit the button to emit it, it turns on and off.

And now the animation doesn’t play , plus only one hand emits the particles.
The only other thing that does work is at the start when
both particles don’t emit as told by the Start function.
Am I missing something here?
heres the script. again any advice will be great!
thanks again for the first part.

#Script Bellow#

function Update ()

{

   if (Input.GetButtonDown ("Fire1"))
        GameObject.Find("flameEmitL").particleEmitter.emit = true;     
        GameObject.Find("flameEmitR").particleEmitter.emit = true;
        animation.CrossFade("cast1");

   if (Input.GetButtonUp("Fire1"))
         
        GameObject.Find("flameEmitL").particleEmitter.emit = false;
        GameObject.Find("flameEmitR").particleEmitter.emit = false;
        animation.CrossFade("idle");

}

function Start ()

     {
     GameObject.Find("flameEmitL").particleEmitter.emit = false;
     GameObject.Find("flameEmitR").particleEmitter.emit = false;
     }

on the guys hand or staff or whatever

function Update(){
var hand : Transform;
var fireParticles : GameObject;
if(Input.GetButtonDown("Fire1")){
Instantiate(fireParticles, hand.position, hand.rotation);
}
}

then on the fireParticles

var chargeTime = 3
var charged = 0;
var fireball : GameObject;
function Start() {
Invoke("charged",chargeTime);
}
function Update(){
if(Input.GetButtonUp("Fire1")){
Destroy(gameObject)
if(charged == 1){
Instantiate(fireball, transform.position, transform.rotation);
}
}
}

function charged () {
charged = 1;
}

so when you click it will create the fire particles and if you release after chargeTime is up it will make a fireball

also you could put this on the fireball to make it shoot

function Update(){
Transform.Translate(Vector3.forward * Time.deltaTime * 30);
}

Guys again. Thanks for you input, you helped me see the problem and resolve it as far as what i was going for.

This isn’t the final script down bellow. But its the effect I am going for…well almost. Only thing to tweek is making an Interrupt while hes casting, also making the fireball release after the spell is cast instead of it releasing after i release the button. that will have to be another Question though.
But for now here is the main body of the script and the idea I was after. Again thanks a lot guys.

var bulletPrefab : Transform;
var explosionPrefab : Transform;

function Update () {

   if (Input.GetKey("1"))
    {
        GameObject.Find("flameEmitL").particleEmitter.emit = true;     
        GameObject.Find("flameEmitR").particleEmitter.emit = true;
        animation.Play("cast1");
        
        
    }
   if (Input.GetKeyUp("1"))
     {    
        GameObject.Find("flameEmitL").particleEmitter.emit = false;
        GameObject.Find("flameEmitR").particleEmitter.emit = false;
        animation.CrossFade("idle");
                
        var bullet = Instantiate(bulletPrefab,
        GameObject.Find("fireballSpawn").transform.position,
        Quaternion.identity);       
        bullet.rigidbody.AddForce(transform.forward * 1000);

     }

}
function Start ()
{
GameObject.Find(“flameEmitL”).particleEmitter.emit = false;
GameObject.Find(“flameEmitR”).particleEmitter.emit = false;
}