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 /
  • Help Room /
This question was closed Oct 05, 2016 at 04:18 PM by RetepTrun for the following reason:

Old and already answered enough

avatar image
5
Question by DarkArtsProdigy · Feb 23, 2013 at 11:44 PM · fpsshooting

Shooting Gun Script

Okay so recently I found this code to make my gun shoot but it doesn't work. There are no errors listed and I'm using the Cartoon SMG from the Asset Store. Here is the code:

 //the bullet we are shooting must have a rigidbody 
 var Bullet : Transform;
 //the speed the bullet is shot at
 var Speed = 16000;
 //where the bullet spawns (most likely the tip of the gun)
 var spawnPoint : Transform;
 //if we shoot like a machinegun or not
 var RapidFire = false;
 //if we shoot every click or not
 var SingleFire = true;
 //this is only used if rapid fire is set to true
 private var shooting = false;
 //RateOfFire private
 var Counter = Time.deltaTime;
 var RateOfFire = 0.250000;
 
 function FixedUpdate () 
 {
 //if single fire is set to true
 if(SingleFire==true){
 //we are using the left mouse button to shoot
 if(Input.GetButtonUp("Fire1")){
 //we create the bullet
 var shot =Instantiate(Bullet, spawnPoint.transform.position, Quaternion.identity); 
 //we add the speed 
 shot.rigidbody.AddForce(transform.forward * Speed); } }
 if(RapidFire ==true){
 if(Input.GetButtonDown("Fire1")){
 shooting=true;
 }
 if(Input.GetButtonUp("Fire1")){
 shooting=false;
 }
 if(shooting==true){ Counter += Time.deltaTime;
 if(RateOfFire
 <
 Counter){
 var shotRapid =Instantiate(Bullet, spawnPoint.transform.position, 
 Quaternion.identity); 
 //we add the speed 
 shotRapid.rigidbody.AddForce(transform.forward * Speed); 
 Counter=0; } } } }

What's wrong? It worked for the guy on Youtube but not for me.

Comment
Add comment · Show 3
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 theundergod · Feb 23, 2013 at 11:51 PM 1
Share

If I may be so bold as to suggest not using an actual instantiated object for this. It's moving so fast that a Raycast might be a much better option.

avatar image eliteslayer · Dec 05, 2013 at 04:40 PM 0
Share

I copied the first script and tried it out and it works perfectly, better then most ive used before

avatar image Thehazmater · Dec 29, 2014 at 06:47 AM 0
Share

this isnt an answer but when i fire the bullet dosent come out of the gun it just stays on the ground why?

4 Replies

  • Sort: 
avatar image
4

Answer by SaiNagata · Jul 19, 2013 at 09:12 AM

Try This, Its Work For Me...

 var Bullet : Transform;
 var Spawn : Transform;
 
 
 function Update ()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         Shot();
     }
 }
 
 function Shot()
 {
     var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
     pel.rigidbody.AddForce(transform.forward * 8000);
     
 
 }
Comment
Add comment · Show 3 · 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 the gamer570 · Feb 01, 2015 at 09:32 PM 0
Share

wouldn't it be - var Bullet : GameObject; var Spawn : GameObject;

that would be for the vars wouldn't it not Transform?

avatar image $$anonymous$$ · May 03, 2016 at 03:08 PM 0
Share

this is better: 'public var Bullet : Transform; public var Spawn : Transform; public var shootSpeed : float; public var AmmoText : UI.Text; public var ClipText : UI.Text; public var Ammo; public var Clips; public var enoughClips; public var enoughAmmo;

function Start() { Clips = 5; Ammo = 10; enoughAmmo = true; enoughClips = true; }

function Update () { if (Ammo <= 0) { enoughAmmo = false; } if(Input.GetButtonDown("Fire1") && enoughAmmo) { Shot(); Ammo = Ammo - 1; } if (Clips <= 0) { enoughClips = false; } if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R) && enoughClips && Ammo < 10) { Clips = Clips - 1; Ammo = 10; enoughAmmo = true; } }

function Shot() { var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation); pel.GetComponent.().AddForce(transform.forward * shootSpeed);

} function FixedUpdate() { AmmoText.text = "Ammo: " + Ammo.ToString(); ClipText.text = "Clips: " + Clips.ToString(); } '

avatar image GreatCorn · Jan 27, 2018 at 08:22 AM 0
Share

Doesn't work for me. Bullets spawn but only change to the X rotation.

avatar image
0

Answer by RetepTrun · Feb 24, 2013 at 12:17 AM

1 It is too hard to read

2 I bet it is not the script but how you have used it, your bullet spawn is probly backwards or your bullet prefab has something wrong with it or something like that

Comment
Add comment · Show 1 · 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 eliteslayer · Dec 05, 2013 at 07:38 PM 0
Share

I tried the shooting script and it worked perfectly, the bullet shot out in front of the muzzle and went straight where ever i shot the gun. There is nothing wrong with the script, just the way you might have been using it.

avatar image
0

Answer by JMgames · Nov 11, 2014 at 11:20 PM

I streamlined the script to shoot full auto only. attach the script to an empty gameobject where you want the bullets to come from.

 var bullet : Transform;  //the bullet we are shooting must have a rigidbody
 var Speed = 32000; //the speed the bullet is shot at
 
 private var shooting = false; //this is only used if rapid fire is set to true
 //RateOfFire private
 var Counter = Time.deltaTime;
 var RateOfFire = 0.5;
 
 function FixedUpdate (){
     
     if(Input.GetButtonDown("Fire1")){
         shooting=true;
     }
     
     if(Input.GetButtonUp("Fire1")){
         shooting=false;
     }
     
     if(shooting==true){ 
     Counter += Time.deltaTime;
     
     if(RateOfFire < Counter){
     var shotRapid =Instantiate(bullet, transform.position, Quaternion.identity);
     shotRapid.rigidbody.AddForce(transform.forward * Speed);
     Counter=0;
     } 
     
 } 
 
 }
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
0

Answer by Plazmoid · Oct 05, 2016 at 04:09 PM

Maybe you're using C# script, but this is Java? (I said MAYBE, so don't hate)

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

Follow this Question

Answers Answers and Comments

19 People are following this question.

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

Related Questions

Damage drop-off over distance on a physical projectile 1 Answer

how to add shoot cooldown 1 Answer

How to script shooting to trigger shooting sound 1 Answer

FPS Gun Hold 0 Answers

Raycast Weapon not cause damage 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