- Home /
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.
Answer by saianjan · Sep 19, 2015 at 03:32 AM
here is right one: if(Input.GetButtonDown("Fire1")|| Input.GetKeyDown(KeyCode.Space)
Answer by DayyanSisson · Oct 04, 2011 at 01:07 AM
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);
}
Your welcome. And also, put comments like this under comments, not answers because it's not an answer.
Hello, what do I have to add when I want the object (fireball) shoot forward?
There's an extra paren after the first clause, here''s the modified code:
if (Input.Get$$anonymous$$eyDown("Fire1") || Input.Get$$anonymous$$eyDown("space"))
Oops, just notice that it should be: if (Input.GetButtonDown("Fire1") || Input.Get$$anonymous$$eyDown("space"))
Your answer
Follow this Question
Related Questions
Flipped objects Rotation issues 1 Answer
Shooing and moving code 1 Answer
How can i fix this shooting code in a 2D plane? 0 Answers
Instantiate projectile at position of specific game object 1 Answer
shooting straight 1 Answer