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 CorruptedTNC · Mar 13, 2013 at 02:53 AM · javascriptraycastreload

Automatic weapon does not stop firing when it runs out of ammo. Using RayCast and Javascript.

I'm attempting to make an m4a1 fire via RayCast. I read through the scripting reference and understand it for the most part however I've run in to a problem. The script I am using is as follows:

 var Range : float = 1000;
 var Force : float = 200;
 var Clips : int = 20;
 var BulletsInClip : int = 60;
 var ReloadTime : float = 2.2;
 var BulletsLeft : int = 0;
 var ShootDelay : float = 0;
 var ShootCooldown : float = 0.2;
 public var audioShot : AudioClip;
 public var audioReload : AudioClip;
 
 function Start()
 {
     BulletsLeft = BulletsInClip;
 }
 
 function Update () 
 {
     if(ShootDelay > 0)
     {
         ShootDelay -= Time.deltaTime;
     }
     else
     {
         ShootDelay = 0;
     }
     if(Input.GetButton("Fire1"))
     {
         if(ShootDelay == 0 && BulletsLeft > 0)
         {
             PlayShot();
             BulletsLeft--;
             CastRay();
             ShootDelay = ShootCooldown;
         }
         if (BulletsLeft == 0 && Clips > 0)
         {
             ShootDelay = ReloadTime;
             BulletsLeft = 0;
             Reload();
         }
     }
 }
 
 function CastRay() 
 {
     var Hit : RaycastHit;
     var DirectionRay = transform.TransformDirection(Vector3.forward);
     Debug.DrawRay(transform.position, DirectionRay * Range, Color.magenta);
     if(Physics.Raycast(transform.position , DirectionRay , Hit, Range ))
     {
         if(Hit.rigidbody)
         {
             Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
         }
     }
 }
 
 function Reload()
 {
     yield WaitForSeconds(ReloadTime);
     PlayReload();
     BulletsLeft = BulletsInClip;
     Clips--;
 }
 
 function PlayShot()
 {
     audio.PlayOneShot(audioShot);
 }
 
 function PlayReload()
 {
     audio.PlayOneShot(audioReload);
 }

However when the gun runs out of ammo, it reloads but continues firing. This causes the gun to continue using up a spare clip until it has a negative amount of clips remaining, which will then stop it from reloading any more.

Any insight you guys can provide would be much appreciated.

Thanks in advance.

Comment
Add comment · Show 2
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 amphoterik · Mar 13, 2013 at 03:14 AM 0
Share

If you hit and release the fire button does it keep firing? Can you be more specific about the error?

avatar image CorruptedTNC · Mar 13, 2013 at 03:54 AM 0
Share

Sorry, I should have specified that this occurs when holding down the fire button. If I release it, it stops firing as expected however as long as the button is held down it will continue to call the reload function until there are 0 or less clips remaining.

2 Replies

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

Answer by Chronos-L · Mar 13, 2013 at 04:08 AM

Your Reload() function is yield multiple time when you keep on holding "Fire1" all the time. That's why you will get negative amount of clips.

I fix it by adding a reloading boolean to the script:

 private var reloading : boolean = false;
 
 function Update () 
 {
     if(ShootDelay > 0)
     {
        ShootDelay -= Time.deltaTime;
     }
     else
     {
        ShootDelay = 0;
     }
 
     //Add this
     if(Input.GetButton("Fire1") && !reloading)
     {
        if(ShootDelay == 0 && BulletsLeft > 0)
        {
          PlayShot();
          BulletsLeft--;
          CastRay();
          ShootDelay = ShootCooldown;
        }
        if (BulletsLeft == 0 && Clips > 0)
        {
          //Add this
          reloading = true;
 
          ShootDelay = ReloadTime;
          BulletsLeft = 0;
          Reload();
        }
     }
 }
 
 function Reload()
 {
     yield WaitForSeconds(ReloadTime);
     PlayReload();
     BulletsLeft = BulletsInClip;
     Clips--;
 
     //Add this
     reloading = false;
 }

Now, after this fix, I can hold on "Fire1" key. It will keep firing until ran of out ammo, reload, consume one clip, then keep going, without me releasing the key at all.

Hopes that this fix will be what you are looking for.

Comment
Add comment · Show 2 · 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 amphoterik · Mar 13, 2013 at 04:14 AM 0
Share

Ah, beat me to it. That should do it.

avatar image CorruptedTNC · Mar 13, 2013 at 08:39 AM 0
Share

Thank you so much, this was driving me nuts.

avatar image
0

Answer by Moonwalker777 · Aug 18, 2013 at 09:39 AM

I've the same problem with a different script , maybe you can put an IF condition like if(animation.IsPlaying("reload"){ bullets = 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

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

13 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

Related Questions

A node in a childnode? 1 Answer

OnGUI nullreference 0 Answers

Raycasting not working as expected 2 Answers

How to Less Than or Equal to with a Raycast? 1 Answer

using Application.OpenURL for opening new tab? 2 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