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 Hankr · Jan 20, 2014 at 02:25 AM · raycastshootingraycastingdelaygun script

How to Fix a Guns Firing Script so it Doesn't Constantly Fire

(Edited question)

Hello everyone! I'm in the process of making a shotgun script where you're supposed to be able to fire a raycast every 1 or so seconds, and the animation is about that long as well, but I'm not sure how to delay the shooting otherwise you can shoot constantly and with infinite ammunition. I would like to have two shots, and when you shoot two, you have to reload, otherwise you hit 'r', and it reloads the gun then as well.

Thanks, Henry

 private var AmmoInGun : int = 2;
 
 var Effect : Transform;
 var TheDammage = 100;
 var Distance = 50;
 var Gun : Transform;
 
 function Update () 
 {
     if (Input.GetKeyDown(KeyCode.R))
     {
         Reload();
     }
     
     var hit : RaycastHit;
     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width * 0.5, Screen.height * 0.5, 0));
     
     if(Input.GetMouseButtonDown(0))
     {
          Shoot();
         
         if (Physics.Raycast(ray, hit, Distance))
         {
             
             var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
             Destroy(particleClone.gameObject, 2);
             hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
 
         }
     
     }
     
         if(AmmoInGun == 0)
         {
             Reload();
         }    
 }
 
 function Shoot()
 {
         audio.Play();
         Gun.animation.CrossFade("Shoot");
         AmmoInGun -= 1;
 
 }
 
 function Reload()
 {
             Gun.animation.CrossFade("RELOAD");
     
 }
Comment
Add comment · Show 1
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 robertbu · Jan 20, 2014 at 02:26 AM 0
Share

There are multiple approaches to solve this problem. Edit your question and include your code to get a fix specific to your situation.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by VildNinja · Jan 20, 2014 at 02:35 AM

you could either move the weapon logic to a coroutine:

 while (true)
 {
 while (!Input.getButtonDown("Fire"))
   yield return new WaitForEndOfFrame();
 
 shoot()
 
 yield return new WaitForSeconds(3.1f)
 }

or you could add a reload counter to your script:

 public float reload;
 
 void Update()
 {
   reload -= Time.deltaTime;
 
   if (reload < 0 && Input.GetButtonDown("Fire"))
   {
     Shoot();
     reload = 3.1f;
   }
 }
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 Hankr · Jan 20, 2014 at 05:16 AM 0
Share

Thank you for the help! although I am a noob, and only really speak java but I'm sure I can figure it out! Thanks again :)

avatar image
0

Answer by Dougbott · Jan 20, 2014 at 02:26 AM

Not to be short with you but coroutine's would do that for you.

Here is link about them.

http://docs.unity3d.com/Documentation/Manual/Coroutines.html

I'm onMy phone. Can you tell. ;)

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 joedartjr · Jan 20, 2014 at 02:32 AM

 public float cooldown = 1f;
 float cooldownRemaining = 0;
 
 void Update
 {
 cooldownRemaining -= Time.deltaTime;
     if(Input.GetMouseButton(0) && cooldownRemaining <= 0)
 {
 *whatever it is you want to do upon fire here*
 cooldownRemaining = cooldown;
 }
 }

This will create a cooldown, and then add the cooldown to a timer for the gun. You can change the "cooldown" variable to be any amount you want; it will be in seconds.

This is obviously the best I can do without seeing your script; and is written in C#, not Javascript, although the principle is the same.

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

21 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

Related Questions

Raycasting Collider Problem 0 Answers

Problem with raycasting in direction of mouse 0 Answers

Raycast shooting in the middle of the screen 1 Answer

scripting problem 0 Answers

How to stop enemies from shooting each other 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