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 Radon · Jul 17, 2012 at 04:16 AM · javascriptfirereloadrate

Help in change the rate of fire.

So I created a code for my gun with reload, rate of fire, range, and fire variables. However, when I reload, the rate of fire goes really high for some reason. Example: http://www.youtube.com/watch?v=PuiOOURL250.

My Code:

 var Spawn : Transform;
 var BulletSpeed : float = 1000;
 var ReloadTime : float = 2;
 var AmmoInMag : float = 30;
 var IsFullAuto = true;
 static var AmmoLeft = 0;
 static var ReloadTTime : float;
 static var IsReloading = false;
 private var CanFire = true;
 var FireRate = 0.1;
 var range = 100;
 var DirtImpact : Transform;
 var ConcImpact : Transform;
 var WoodImpact : Transform;
 var BulletBulletHole : Transform;
 var WaterImpact : Transform;
 var MetalImpact : Transform;
 
 
 function Start () {
  AmmoLeft = AmmoInMag;
  ReloadTTime = ReloadTime;
 
 }
 
 function Update (){
 
  if (Input.GetKeyDown(KeyCode.R))
  {
  BroadcastMessage("ReloadAnim");
  Reload(); 
  }
 
  if(IsFullAuto == false){
  if(Input. GetButtonDown("Fire1")){
  if(AmmoLeft > 0){
  BroadcastMessage("FireAnim");
  Fire();
  } 
  }
  }
  else{
  if(Input. GetButton("Fire1")){
  if(AmmoLeft > 0){
  BroadcastMessage("FireAnim");
  Fire();
  } 
  }
  }
  
  if(AmmoLeft == 0)
  {
  Reload();
  }
  
  if(AmmoLeft < 0){
  AmmoLeft = 0;
  }
 }
 
 function Fire(){
  if(CanFire == true && IsReloading == false){
  
  var hit : RaycastHit;
  var fwd = Spawn.TransformDirection(Vector3.forward);
  Debug.DrawRay(Spawn.position, fwd);
  
  CanFire = false;
  yield WaitForSeconds(FireRate);
  CanFire = true;
  AmmoLeft -= 1;
  audio.Play();
  
  if(Physics.Raycast(Spawn.position,fwd,hit,range)){
   var hitTag = hit.collider.tag;
   var pos = hit.point+0.01*hit.normal; 
   var rot = Quaternion.FromToRotation(Vector3.forward, hit.normal);
   if(hitTag == "Dirt"){
    Instantiate(DirtImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
   }
   if(hitTag == "Concrete"){
    Instantiate(ConcImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    }
    
   if(hitTag == "Wood"){
    Instantiate(WoodImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    }
   if(hitTag == "Water"){
    Instantiate(WaterImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    }
   if(hitTag == "Metal"){
    Instantiate(MetalImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    } 
  }
  }
 }
 
 function Reload(){
  CanFire = false;
  IsReloading = true;
  BroadcastMessage("ReloadAnim");
  yield WaitForSeconds (ReloadTime);
  IsReloading = false;
  CanFire = true;
  AmmoLeft = AmmoInMag;
 }
Comment
Add comment · Show 7
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 twoface262 · Jul 17, 2012 at 04:20 AM 0
Share

Please fix the format. I can't read your code.

avatar image Radon · Jul 17, 2012 at 04:39 AM 0
Share

I just edited it

avatar image Bovine · Jul 17, 2012 at 04:57 AM 0
Share

The indentation is still all over the place, please try editing it again.

Id this your script or an edited script you've downloaded?

avatar image Radon · Jul 17, 2012 at 05:02 AM 0
Share

What are you talking about?

avatar image Bovine · Jul 17, 2012 at 05:12 AM 0
Share

Hmmm the indentation of your code is not consistent with braces ending at the same level of indentation on adjacent lines and it not being clear where one area of scope begins and ends?

Show more comments

1 Reply

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

Answer by Bovine · Jul 17, 2012 at 05:29 AM

I'm not sure why Fire() is a coroutine - you seem to be using CanFire as a lock around firing, but you're firing many coroutines potentially that will wait and then fire. There's also no check after your WaitForSeconds() to see whether there's any ammo left - you also deplete the ammo after the wait which I feel sure is going to lead to problems.

I'm suspicious as to why you need the check if AmmoLeft < 0 - i suspect your ammo keeps going negative and you've thrown this in to fix it, but this is something of a bandaid of the real problem.

Personally I would take the locking out, other than around reloading and replace the coroutine with a simple check for elapsed time, so you might have a variable so:

 private var NextFireAt : float = -1;

Which in Awake() you would set:

 NextFireAt = Time.time;
     

To capture when the player can next fire, then in your Update()

 if(Time.time >= NextFireAt)
 {
     // do your check whether the key is down/has been hit here
     // you could also check for ammo FIRST before
     // checking IsFullAuto and for any key presses
 }

And then get rid of the coroutine in Fire() as you don't need a wait, your Update() is now limiting the rate of fire, but when you enter the Fire() routine you need to set:

 NextFireAt = Time.time + FireRate;

In Update() at the top you can probably then:

 if(IsReloading) return;

Hopefully that helps.

Apologies if the code isn't quite right as I'm a C# bod really, but it should be obvious what I am suggesting I hope!

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 Radon · Jul 17, 2012 at 05:37 AM 0
Share

Dude, that was awesome. Thanks a lot. It worked.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Realistic reloading system 1 Answer

Fooling Rate Of Fire C# 1 Answer

Creating fire particle in code 1 Answer

separate script to reload. 0 Answers

rapid movement back and forth 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