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 Stormy102 · Apr 04, 2014 at 05:45 PM · javascriptfpsgunmachine

How can I convert this script to automatic fire?

I have this script that I am using for rifles and pistols. But I would like it to work in automatic fire. None of my attempts so far have worked. Please could someone please add it in without making it too complicated. Here is the script

 #pragma strict
 
 var Effect : Transform;
 var TheDammage = 100;
 var gunshotSound : AudioClip;
 var reloadGrenade : AudioClip; //sound clip to reload our grenade launcher
 var ammoCount : int = 10; //how much ammo in our clip
 var totalAmmo : int = 120; //how much total ammo we have available
 var reloadTime : int = 3; //how long it takes before we've reloaded
 var reloadAmount : int = 10; //how many grenades we'll reload
 var ammo : GUIText; //our GUI text that displays our ammo in text format
 var fireRate : float = 3.0;
 var bulletHole : Material;
 var force = 10;
 private var nextFire : float =0.0;
  
 function Update ()
 { 
     Fire ();
     Reload();
     OnGUI();
 }
  
 function Fire () {
  
     if (ammoCount > 0) {
  
         if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
  
             nextFire = Time.time + fireRate;
  
             InstantiateGrenade();
  
             ammoCount -=1;
  
         }
  
     }
  
 }
  
 //---------------------------------------------------------------------------------
  
 function InstantiateGrenade () {
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
     
     if (Input.GetMouseButtonDown(0))
     {
         AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 1);
         if (Physics.Raycast (ray, hit, 500))
         {
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             Instantiate(bulletHole, hit.point, hitRotation);
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
         }
     }
 }
  
 //---------------------------------------------------------------------------------
  
 function Reload () {
  
     if(ammoCount < 1 && totalAmmo > 10) { //if we have less than 1 bullet then we can reload
  
     if(Input.GetKeyDown("r")) {
  
         AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1); //plays reload soundclip
  
         yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
  
 ammoCount += reloadAmount; //adds ammo to our "clip" based off the reloadAmount
  
 totalAmmo -= reloadAmount; //subtracts whatever the reloadAmount was from our total ammo every time we reload
  
 }
  
 }
  
 }
  
 //---------------------------------------------------------------------------------
  
 function OnGUI () {
  
 ammo.text = "Ammo: " + ammoCount + "/" +  totalAmmo.ToString(); //displays our GUI Text in the format we want. The sections in "" will appear exactly as their written
  
 }
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
0
Best Answer

Answer by Stormy102 · Apr 05, 2014 at 10:50 AM

Thanks. I tried that, but the rate of fire was too fast. It blew all of my rounds in a few seconds. Is there anything that could slow it down?

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 Stormy102 · Apr 05, 2014 at 03:00 PM 0
Share

I managed to get it working. I simply tweaked it a litle bit. To change the rate of fire, modify the "fireRate" variable. If anyone wants the whole script for an automatic raycasting script, here it is:

 #pragma strict
 
 var Effect : Transform;
 var TheDammage = 100;
 var gunshotSound : AudioClip;
 var reloadGrenade : AudioClip; //sound clip to reload our grenade launcher
 var ammoCount : int = 47; //how much ammo in our clip
 var totalAmmo : int = 235; //how much total ammo we have available
 var reloadTime : int = 5; //how long it takes before we've reloaded
 var reloadAmount : int = 47; //how many grenades we'll reload
 var ammo : GUIText; //our GUI text that displays our ammo in text format
 var gunName : GUIText;
 var fireRate : float = 0.1;
 var bulletHole : $$anonymous$$aterial;
 var force = 10;
 private var nextFire : float =0.0;
 public var spreadFactor : float = 0.02;
  
 function Update ()
 { 
     Fire ();
     Reload();
     OnGUI();
 }
  
 function Fire () {
 
     if (Input.GetButton("Fire1") && Time.time > nextFire) {
         
         if (ammoCount > 0) {
  
             nextFire = Time.time + fireRate;
  
             InstantiateGrenade();
  
             ammoCount -=1;
  
         }
  
     }
  
 }
  
 //---------------------------------------------------------------------------------
  
 function InstantiateGrenade () {
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
     
     if (Input.Get$$anonymous$$ouseButton(0))
     {
         AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 1);
         if (Physics.Raycast (ray, hit, 1000))
         {
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
             Instantiate(bulletHole, hit.point, hitRotation);
             Destroy(particleClone.gameObject, 2);
             hit.transform.Send$$anonymous$$essage("ApplyDammage", TheDammage, Send$$anonymous$$essageOptions.DontRequireReceiver);
         }
     }
 }
  
 //---------------------------------------------------------------------------------
  
 function Reload () {
  
     if(ammoCount < 1 && totalAmmo > 10) { //if we have less than 1 bullet then we can reload
  
     if(Input.Get$$anonymous$$eyDown("r")) {
  
         AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1); //plays reload soundclip
  
         yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
  
 ammoCount += reloadAmount; //adds ammo to our "clip" based off the reloadAmount
  
 totalAmmo -= reloadAmount; //subtracts whatever the reloadAmount was from our total ammo every time we reload
  
 }
  
 }
  
 }
  
 //---------------------------------------------------------------------------------
  
 function OnGUI () {
  
 ammo.text = "Ammo: " + ammoCount + "/" +  totalAmmo.ToString(); //displays our GUI Text in the format we want. The sections in "" will appear exactly as their written
 gunName.text = "Lewis Gun $$anonymous$$ark I";
 
 }
avatar image
0

Answer by roojerry · Apr 04, 2014 at 05:47 PM

Try Input.GetButton instead of GetButtonDown. It will return true as long as the button is held instead of only once like GetButtonDown

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

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

22 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 avatar image avatar image avatar image

Related Questions

Gun Firing help? 2 Answers

How to let my gun shoot ?? 0 Answers

Some issues with weapon switching and reloading? 2 Answers

crosshair scrip not working 1 Answer

Need help with bullet/gun scripting 3 Answers


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