- Home /
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.
nyways 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!
Back to the emitter thing were do you put the line of code?
Answer by Bicko · Jan 20, 2012 at 03:26 PM
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:
GetButtonDown: Spawn the fireball, and enable the particle emitter on his hands
GetButton: Make it grow bigger for as long as this is running
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!
Answer by sinokreal · Jan 22, 2012 at 03:23 AM
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;
}
Answer by sinokreal · Jan 22, 2012 at 03:22 AM
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;
}
Answer by sam32x · Jan 22, 2012 at 04:03 AM
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);
}
Answer by sinokreal · Jan 23, 2012 at 06:36 PM
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; }
If you want to interrupt the effect, then you could create a boolean variable along the lines of beenInterrupted, set it to false by default.
Then have it so that when the player is interrupted (takes damage, for example) you could Reset Input, although I'm not sure if this will work.. but the documentation says it "Resets all input" so I imagine it will. You may still have a problem with your Get$$anonymous$$eyUp working though.. in this case you'll want to change your if statement to:
if (Input.Get$$anonymous$$eyUp("1") && !beenInterrupted)
So that it tests to see if the player has been interrupted. Note that I'm not using "beenInterrupted == false" -putting a '!' at the front of the variable does this for you! After this, you'll need to reset beenInterrupted back to false, so just add this immediately after your key up if statement:
else
beenInterrupted = false;
Another thing to note: if you're doing a statement that only does 1 thing, you don't need to encase it in parentheses. This will just keep your code smaller so you can browse through it faster.
Your answer
Follow this Question
Related Questions
Shoot particles like motorcycle roost... 0 Answers
Laser Beam in Unity 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to change particles' view? 2 Answers