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 goten000023 · Aug 30, 2014 at 06:39 PM · javascriptraycastgunnoobammo

musket gun script is not working

this script is supposed to make the gun shoot once and then reload but it just lets me keep shooting i am pretty much a beginner with scripting

here is the script

   var reloadTime = 5;
   var totalAmmo = 10;
   var ammoCount = 1;
                                                                                                                                    
   function Update ()   {                                   
          
      if (ammoCount >= 1) {
        CanShoot = true;
      }
      
      if (ammoCount <= 0) {
      CanShoot = false;
      }
      
      if (CanShoot == false) {
      reload();
      }
      
      if (CanShoot == true) { 
      Shoot();
      }
      }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
   function Shoot () {
   
   var hit : RaycastHit;
   var ray: Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
 
   if (Input.GetMouseButtonDown(0))
   {
   if (Physics.Raycast (ray, hit, 100))
   {
   hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
   {ammoCount -= 1;
 }
 }
 }
 } 
  
  function reload () {
  
  if (Input.GetKeyDown("r")) {
  
  yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
  
  ammoCount += 1; //adds ammo to our "clip" based off the reloadAmount
  
  totalAmmo -= 1; //subtracts whatever the reloadAmount was from our total ammo every time we 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 everett24 · Aug 30, 2014 at 08:40 PM 0
Share

Try setting canShoot to false after your shot, maybe its not setting it, also when you declare it have it set to false

3 Replies

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

Answer by froYo · Aug 30, 2014 at 09:56 PM

It looks to me like you are only reducing the ammo count if your raycast is detecting a hit, whereas it should subtract ammo regardless of a hit.

 function Update ()   
 {                                   
     if (ammoCount >= 1) 
     {
         CanShoot = true;
     }
 
     if (ammoCount <= 0) 
     {
         CanShoot = false;
     }
 
     if (CanShoot == false) 
     {
         reload();
     }
 
     if (CanShoot == true) 
     { 
         Shoot();
     }
 } 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
 function Shoot () 
 {
 
     var hit : RaycastHit;
     var ray: Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
 
     if (Input.GetMouseButtonDown(0))
     {
         if (Physics.Raycast (ray, hit, 100))
         {
             hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
         }
         ammoCount -= 1;
     }
 } 
  
  function reload () 
  {
      if (Input.GetKeyDown("r")) 
      {
          yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
          
          ammoCount += 1; //adds ammo to our "clip" based off the reloadAmount
          
          totalAmmo -= 1; //subtracts whatever the reloadAmount was from our total ammo every time we reload
     }                                  
 }

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
Wiki

Answer by BMayne · Aug 30, 2014 at 11:51 PM

Hey There,

You can make your code a lot simpler by removing that can fire checks that you are doing. I have made a quick example of a simple firing script. It is written in C# so you will have to convert it (no copying and pasting :) ) it should not take to long.

 float currentAmmo = 5.0f;
 float currentReloadTime = 0.0f;
 bool isReloading = false;
 
 const float RELOAD_TIME = 2.0f;
 const int MAX_AMMO = 8; 
 
 
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.R))
     {
         if (currentAmmo != MAX_AMMO && !isReloading) // if we already have max ammo lets leave or if we are already reloading
         {
             isReloading = true; // We are now reloading
             currentReloadTime = RELOAD_TIME; //Set our current load time 
         }
     }
 
     if (isReloading)
         ReloadStep();
     else
         GunStep();
 
  
 }
 
 void GunStep()
 {
     if (currentAmmo <= 0)
         return; //No ammo so we leave.
 
     if (!Input.GetMouseButtonDown(0)) //the '!' means Not. so the mouse button is Not Down 
         return; //We are not firing so we don't need to be here
 
     //We fired our gun so we need to use a bullet 
     currentAmmo--;
 
     //Start our making our raycast
     RaycastHit hit;
     Ray ray = Camera.main.ScreenPointToRay(new Vector3((float)Screen.width * 0.5f, (float)Screen.height * 0.5f, 0.0f));
 
     //Fire the ray and tell anything we hit to apply damage
     if (Physics.Raycast(ray, out hit, 100))
     {
         hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
     }
 }
 
 void ReloadStep()
 {
     currentReloadTime -= Time.deltaTime; //We count down
 
     if( currentReloadTime <= 0.0f )
     {
         isReloading = false;
         currentAmmo = MAX_AMMO;
     }
 }

As you can see you don't need much more then that. Let me know if you have any questions.

Regards,

Edited: Added reloading

Comment
Add comment · Show 7 · 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 goten000023 · Aug 31, 2014 at 12:05 AM 0
Share

i see but there is no reloading here

avatar image goten000023 · Aug 31, 2014 at 12:14 AM 0
Share

do understand what you mean though

avatar image BMayne · Aug 31, 2014 at 12:18 AM 0
Share

Boom reloading!

Also as I posted this I noticed I put constants you don't really have to worry about them if you don't know what they are. They are just values that can't be altered by code.

avatar image goten000023 · Aug 31, 2014 at 01:49 AM 0
Share

um i am having trouble fully understanding this is it possible you could post this in unityscript

avatar image BMayne · Aug 31, 2014 at 01:52 AM 0
Share

I am not the best with UnityScript, C# developer here :(

What part are you stuck on?

Show more comments
avatar image
0

Answer by goten000023 · Aug 31, 2014 at 12:25 AM

thank you all very much i sorted it all out

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

ammo display javascript 1 Answer

Need coding help with fps 0 Answers

How do I make a gun? 1 Answer

How to send a raycast to an object to give me ammo? 0 Answers

need help converting gun script from c# to javascript 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