Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Deelis · Jun 07, 2011 at 06:29 PM · fpsgamegunshooterreloading

Fps game gun reloading problem

I'm making a FPS game, but reloading won't work!I'm writing it with JavaScript Can anyone help?Code:

 public var BulletPrefab : Transform;
 public var BulletSpeed : float = 6000;
 public var GunMuzzle : Transform;
 public var ClipSize : float = 30;
 public var Clip : float = 30;
 public var ReloadTime : float = 2.0;
 
 function Update () {
     if (Input.GetButtonDown("Fire1")) { 
         if (!BulletPrefab | !BulletSpeed ) {
             Debug.Log("[shoot] Undefined Variables");
             
         } else {
             if (Clip >0) {
             var BulletSpawn = Instantiate (BulletPrefab,GunMuzzle.transform.position,Quaternion.identity);
             BulletSpawn.rigidbody.AddForce(transform.forward * BulletSpeed );
             Clip -= 1;
         }
     }    
 }
 
     
 }
 if (Clip < 1 ){
         Debug.Log("Reloading");
         yield WaitForSeconds (ReloadTime) ;
         Clip = ClipSize ;
         
 }

Comment
Add comment · Show 4
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 almo · Jun 07, 2011 at 07:01 PM 1
Share

Rule #1 of Unity Answers: format your code so we can read it.

avatar image FLASHDENMARK · Jun 07, 2011 at 07:05 PM 0
Share

Yes! Format your code by highlighting the code an press the "10101" button. I already did this for you.

avatar image Anxo · Jun 07, 2011 at 07:09 PM 0
Share

please edit your question to properly format the code. We will be happy to answer your question but please dont make us struggle to figure out whats going on.

avatar image Deelis · Jun 08, 2011 at 05:50 AM 0
Share

Oh my god I'm sorry guys. T_T I'mma noob

1 Reply

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

Answer by Anxo · Jun 07, 2011 at 07:16 PM

you cant have a yield in an update function. What you can do is use a while loop in your start function.

 function Start(){
      while(true)
      {
           while(!Reloading) yield;
           Debug.Log("reloading");
           yield WaitForSeconds(ReloadTime);
           Clip = ClipSize;
           Reloading = false;
      }
 
 }
 
 function Update(){
      if(Input.GetButtonDown("Fire1"))
      {
             Reloading = true;
      }
 }

please remember that you will get a much faster answer if you ask your question in a way that does not give people a hard time ot figure out what the problem is.

Comment
Add comment · Show 4 · 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 Chris D · Jun 07, 2011 at 08:42 PM 0
Share

I'm trying to wrap my head around this; does your first yield statement kick out to the Update(), run through one frame, then resume?

avatar image Anxo · Jun 08, 2011 at 12:03 AM 1
Share

my first while statement just makes sure that the start function loops. As you know Chris, Start only runs once just before the update function. so the while(true) which is always true will cause the while to loop all game long. Without a yield or some sort of delay, a while true in start will crash your game/editor but with a delay it works well. the second while tells it not to go on unless reloading is true, when reloading becomes true it will pass that line of code and start to reload. at the end reloading is set back to false so it will only run reload once till reloading is set back to true externally.

avatar image Deelis · Jun 08, 2011 at 06:10 AM 0
Share

OH $$anonymous$$Y GOD!Thank you, Thank you! It Works now :D Thanks again

avatar image Chris D · Jun 08, 2011 at 04:08 PM 0
Share

@Anxo Ah, excellent! I know about Start() only running once, but I haven't dealt with yield before. Thanks for the explanation.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity FPS Rocket Launcher Light and Sound Effect 0 Answers

Unity Iphone Build keeps crashin 1 Answer

FPS animations for different purposes? 1 Answer

Any problem with using real weapons in a FPS game? 0 Answers

.Js Particle issue (probably easy for moderate coders) 0 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