x


How can i shoot with the spacebar

How can i shoot whith the space bar. I am making a 3rd person and i want my character to shoot fire out of its mouth. I made a script from a tuorial and it wont work.Can anyone help me by putting up a script.

more ▼

asked May 24 '10 at 01:18 AM

user-2679 (yahoo) gravatar image

user-2679 (yahoo)
1 2 2 4

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Alright, it would help if you posted up your script. You really shouldn't ask people to write scripts for you, or even put them up for you. The is to help with your problem, not give you code. What Cristian Stewart said was correct, but obviously that's not the whole thing. This will probably be the only time someone will help you by giving you a script, but I'm going to explain things to you:

First, use the default code that's already set up:

function Update (){
}

The Update function is called twice a frame. Next we have to add the variables that you would need:

var fireBall : GameObject;
var shootPoint : Transform;

function Update (){
    }

"fireBall" needs to be a GameObject. "shootPoint" needs to be a transform because that's where the fireBall is going to come out. If you want it to come out of his mouth, put an empty gameObject near his mouth. Make sure the shootPoint is a child of the object that has the script. Now for the actual code that makes it happen:

var fireBall : GameObject;
var shootPoint : Transform;

 function Update (){
        if(Input.GetKeyDown("Fire1")) || Input.GetKeyDown("space")){
Instantiate(fireBall, shootPoint.position, tranform.rotation);
     }

The first line of code, the "if" statement, means that whatever below will only be executed if anything in the if statement is met. The Input.GetKeyDown("Fire1")), means if the input: "Fire1" (Left Mouse Click) is down, it will execute the code.

if(Input.GetKeyDown("Fire1"))

The completed line is above. Next, we want it to shoot if they click, OR if they hit "space". To do that we need to put in the rest of the if statement:

if(Input.GetKeyDown("space"))

To make sure it means OR, we need these symbols ||. || means OR. So the completed line is:

if(Input.GetKeyDown("Fire1")) || Input.GetKeyDown("space"))

To keep the script organized, and to make sure the if statement doesn't mess with any other code, we put in brackets:

if(Input.GetKeyDown("Fire1")) || Input.GetKeyDown("space")){
}

Now the conditions are set we need it to actually DO something. For that we INSTANTIATE something. Instantiate means "create" after we write Instantiate, we need to write what's instantiate, and how it's going to be displayed. So the first part inside the parenthesis is what's going to be instantiated, in this case, the fireBall:

Instantiate(fireBall, xxxxx,xxxxx);

Next, we need to display where it's going to be shot from. Normally, it's written like this:

Instantiate(fireBall, transform.position, xxxxxxx)

With that, it will be shot from the center of the object it's attached to. But to make it come from a specific place we write it like this:

Instantiate(fireBall, shootPoint.position, xxxxxxx)

Now we need to make sure it's rotation is displayed too:

Instantiate(fireBall, shootPoint.position, tranform.rotation)

With the above code, the fireball will have no rotation, which is fine.

So that's how it works. I really hoped this helped. The completed script is this:

var fireBall : GameObject;
var shootPoint : Transform;

 function Update (){
        if(Input.GetKeyDown("Fire1")) || Input.GetKeyDown("space")){
Instantiate(fireBall, shootPoint.position, tranform.rotation);
     }
more ▼

answered Oct 04 '11 at 01:07 AM

Overlord gravatar image

Overlord
484 67 78 88

(comments are locked)
10|3000 characters needed characters left

this clears a lot thx nighthawx

more ▼

answered Oct 04 '11 at 02:49 AM

rohan bhangui gravatar image

rohan bhangui
16 9 12 13

Your welcome. And also, put comments like this under comments, not answers because it's not an answer.

Oct 04 '11 at 03:43 AM Overlord
(comments are locked)
10|3000 characters needed characters left

Yes, this is easy. Here's a simple shoot script.

I assume the one your using uses something like the following

if(Input.GetButtonDown("Fire1")){
    //some fire coding

What you want to do to enable spacebar is this -

if(Input.GetButtonDown("Fire1") || Input.GetKeyDown("space")){

you see?

The || means "or" - so if either of them are pushed it will fire. The space part will allow it to trigger when you hit space.

Keep looking at the tutorials etc to get started, Christian Stewart

more ▼

answered May 24 '10 at 02:05 AM

Christian Stewart gravatar image

Christian Stewart
165 13 16 21

It's recommended to not use GetKeyDown and instead us the Input Manager so that keys can be rebound on the user side. See documentation here: http://unity3d.com/support/documentation/Manual/Input.html

Jun 07 '10 at 02:47 AM Tetrad
(comments are locked)
10|3000 characters needed characters left

If you want your character to breathe fire, this is probably the best way. Make a particle emitter and place it where in the mouth you want the fire to come from. Assign the particle material to fire (built into unity) and set the force, animation, etc properties as you wish. In order to make the emitter emit when the player is pressing down the spacebar, you would use this:

WhateverYouCalledYourParticleEmitter.emit = false

funtion Update() { if(Input.GetButton("Jump")) { WhateverYouCalledYourParticleEmitter.emit = true } }

Hope I was of help to ya :)

~Choco

more ▼

answered May 24 '10 at 02:05 AM

Jon 1 gravatar image

Jon 1
19 4 5 10

i tried your scirpt i get errors regarding semicolons and other stuff.....could you write your code again or soemthing

Oct 03 '11 at 11:59 PM rohan bhangui
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x329
x8

asked: May 24 '10 at 01:18 AM

Seen: 4421 times

Last Updated: Oct 04 '11 at 03:43 AM