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 /
This question was closed Aug 22, 2014 at 06:54 AM by HexadimensionalerAlp for the following reason:

Other

avatar image
0
Question by HexadimensionalerAlp · Apr 26, 2014 at 07:17 AM · fpsshootingdelayreloadkeycode

Gun reload and clip size

First Problem: I can only shoot 6 times instead of 10.

Second Problem: When the clip is empty I can't shoot anymore because it doesn't reload. I think that the yield is the problem.

Third Problem: 'GetButtonDown(KeyCode.R)' doesn't work although monodevelop recommend it

..............................................................................................................................................................

 var projectile : Rigidbody;
 
 var speed = 5;
 
 var fireRate : float = 0.1;
 
 var clone : Rigidbody;
 
 var weapon : GameObject;
 
 var clipsLeft : int = 5; 
 var clipSize : int = 10;    
 
 private var ammo : int;
 
 private var nextFire = 0.0;
 
 private var wait : boolean;
 
 private var reloadTime : float = 1.5;
 
 
 function Start () 
 
 {
 Screen.showCursor = false;
 
 ammo = clipSize;
 
 }
 
 function Update () {
 
 if(Input.GetButton("Fire1") && Time.time > nextFire && wait == false)
 
 {
 
 if(ammo > 0){
 
 nextFire = Time.time + fireRate;
 
 clone = Instantiate(projectile, transform.position, transform.rotation);
 
 clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
 
 Destroy (clone.gameObject, 5);
 
 audio.Play();
 
 ammo--;
 
 if(ammo <= 0)
 
 {
 
 Reload ();
 
 }
 
 }
 
 }
 
 if(Input.GetButtonDown("R"))
 
 {
 
 Reload ();
 
 }
 
 }
 
 
 
 function Reload () {
 
     if(clipsLeft > 0) {
 
     if (ammo >0){
 
         clipsLeft--;
 
         wait = true;
 
         audio.play();
 
         yield WaitForSeconds(reloadTime);
 
         ammo = clipSize + 1;
 
         wait = false;
 
         }
 
         else {
 
         clipsLeft--;
 
         wait = true;
 
         audio.play();
 
         yield WaitForSeconds(reloadTime);
 
         ammo = clipSize;
 
         wait = false;
 
         }
 
     }
 
 }
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 robertbu · Apr 26, 2014 at 07:19 AM 0
Share

For your third problem, use Input.Get$$anonymous$$eyDown():

  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) {
avatar image robertbu · Apr 26, 2014 at 07:50 AM 0
Share

Given the double spacing and lack of indentation, your code was difficult to read, and I could not see any problems that would result in the errors you are having. So I cleaned the code up a bit. The only issue I found was the low setting 'fireRate'. Being small, I was getting two overlapping/colliding projectiles. When I change the value to 0.2, the two other issues you mention work...fired a full 10 rounds, paused and reloaded when empty. Here is the code I tested:

 #pragma strict
 
 var projectile : Rigidbody;
 var speed : float = 5.0;
 var fireRate : float = 0.1;
 var weapon : GameObject;
 var clipsLeft : int = 5; 
 var clipSize : int = 10;    
 
 private var clone : Rigidbody;
 private var ammo : int;
 private var nextFire = 0.0;
 private var wait : boolean;
 private var reloadTime : float = 1.5;
  
  
 function Start () 
 {
     Screen.showCursor = false;
     ammo = clipSize;
 }
  
 function Update ()
 {
     if (Input.GetButton("Fire1") && (Time.time > nextFire) && wait == false)
     {
         if(ammo > 0)
         {
             nextFire = Time.time + fireRate;
             clone = Instantiate(projectile, transform.position, transform.rotation);
             clone.velocity = transform.forward * speed;
             Destroy (clone.gameObject, 5);
             //audio.Play();
             ammo--;
             if(ammo <= 0)
             {
                 Reload ();
             }
         }
     }
      
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R))
     {
         Reload ();
     }
 }
  
 function Reload ()
 {
     Debug.Log("Reload");
 
     if(clipsLeft > 0)
     {
         if (ammo >0)
         {
             clipsLeft--;
             wait = true;
             audio.Play();
             yield WaitForSeconds(reloadTime);
             ammo = clipSize + 1;
             wait = false;
      
         }
         else
         {
             clipsLeft--;
             wait = true;
             audio.Play();
             yield WaitForSeconds(reloadTime);
             ammo = clipSize;
             wait = false;
         }
     }
 }
avatar image HexadimensionalerAlp · Apr 26, 2014 at 10:43 AM 0
Share

thank you for your quick answer the problem with keycode.r was that I wrote 'getBUTTON' ins$$anonymous$$d of 'get$$anonymous$$EY' the clipsize was 6 in the object inspector don't know why. the reload problem was caused by audio.Play() don't know why

0 Replies

  • Sort: 

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

Reload Ammo is not working 1 Answer

adding a delay to shooting 2 Answers

Reload help 1 Answer

Reload Ammo is not working 0 Answers

Rayshoot Reload Problem (Can still shoot) 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