Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by user-2679 (yahoo) · May 24, 2010 at 01:18 AM · shootspacebar

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by saianjan · Sep 19, 2015 at 03:32 AM

here is right one: if(Input.GetButtonDown("Fire1")|| Input.GetKeyDown(KeyCode.Space)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
11

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);
      }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rohan bhangui · Oct 04, 2011 at 02:49 AM 0
Share

this clears a lot thx nighthawx

avatar image DayyanSisson rohan bhangui · Oct 04, 2011 at 03:43 AM 0
Share

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

avatar image WardPeeters · Apr 02, 2015 at 04:24 PM 0
Share

Hello, what do I have to add when I want the object (fireball) shoot forward?

avatar image thorp · Jun 03, 2015 at 08:21 PM 0
Share

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"))
avatar image thorp · Jun 03, 2015 at 09:14 PM 0
Share

Oops, just notice that it should be: if (Input.GetButtonDown("Fire1") || Input.Get$$anonymous$$eyDown("space"))

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges