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 20, 2012 at 04:05 AM · javascripttimergun

Firing Cooler

Hi, I tried to make a fire cooler, in which my hand gun shoots, the animation work, the raycast works, then it has to cool off for 0.5 seconds in order to re-fire. So I tried the following. I created a variable that is similar to my rate of fire and called it "ShootCooler, and then set it equal to each other after the fire function. But that didn't work. Here is my code without my trials:

 NextFireAt = Time.time;
 var Spawn : Transform;
 var BulletSpeed : float = 1000;
 var ReloadTime : float = 1;
 var AmmoInMag : float = 7;
 var IsFullAuto = true;
 static var AmmoLeft = 0;
 static var ReloadTTime : float;
 static var IsReloading = false;
 private var CanFire = true;
 var FireRate = 0.3;
 var range = 100;
 var DirtImpact : Transform;
 var ConcImpact : Transform;
 var WoodImpact : Transform;
 var BulletBulletHole : Transform;
 var WaterImpact : Transform;
 var MetalImpact : Transform;
 var BloodImpact : Transform;
 private var NextFireAt : float = -1;
 
 
 function Start () {
  AmmoLeft = AmmoInMag;
  ReloadTTime = ReloadTime;
 
 }
 
 function Update (){
  if(IsReloading) return;
  if(Time.time >= NextFireAt)
 
  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);
    }
   if(hitTag == "Metal"){
    Instantiate(MetalImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    } 
     if(hitTag == "Blood"){
    Instantiate(BloodImpact,pos,rot);
    } 
  }
  }
 }
 
 function Reload(){
  CanFire = false;
  IsReloading = true;
  BroadcastMessage("ReloadAnim");
  yield WaitForSeconds (ReloadTime);
  IsReloading = false;
  CanFire = true;
  AmmoLeft = AmmoInMag;
 }
Comment
Add comment
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

1 Reply

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

Answer by Seth-Bergman · Jul 20, 2012 at 04:18 AM

seems to me that's already handled in this script.. in the Fire function, with

 if(CanFire && !IsReloading) 
 
 ..(you don't need the &quot;== true&quot;)
 
 the value of the var FireRate would determine the rate of fire..
 
 EDIT___________
 

The fire function only gets entered once every .3 seconds, due to the FireRate.. If you move the lines

 yield WaitForSeconds(FireRate);
  CanFire = true;
 

to the end, and put all your animation, sound, etc. calls above that, It will all only be called once every .3 seconds (or whatever the rate is set to).. isn't "Rate of fire" and "cool down time" the same thing?

as it stands now, the FireRate regulates both the cool down time between shots, AND the time it takes the bullet to reach its target.. so if that's what you intended, you could just add a second yield:

 function Fire(){
 
  if(CanFire && !IsReloading){
 
  var hit : RaycastHit;
  var fwd = Spawn.TransformDirection(Vector3.forward);
  Debug.DrawRay(Spawn.position, fwd);

  CanFire = false;
   
 //initial animation code,sound etc. probably goes here

  //if you wanted a delay before bullet hits, otherwise move next line to bottom
  //yield WaitForSeconds(FireRate);
 //This is what caused the delay, because it comes before the raycast etc.
 
  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 == &quot;Dirt&quot;){
    Instantiate(DirtImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
   }
   if(hitTag == &quot;Concrete&quot;){
    Instantiate(ConcImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    }
 
   if(hitTag == &quot;Wood&quot;){
    Instantiate(WoodImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    }
   if(hitTag == &quot;Water&quot;){
    Instantiate(WaterImpact,pos,rot);
    }
   if(hitTag == &quot;Metal&quot;){
    Instantiate(MetalImpact,pos,rot);
    Instantiate(BulletBulletHole,pos,rot);
    } 
     if(hitTag == "Blood"){
    Instantiate(BloodImpact,pos,rot);
    } 
  }
   //could have "cool down code" here, if you need..
     yield WaitForSeconds(coolDownRate);
     CanFire = true;
  }
 }

Comment
Add comment · Show 8 · 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 20, 2012 at 04:22 AM 0
Share

the rate of fire works fine. I just want to add cool off to the weapon. A period of time that takes to redo the fire function and eventually the fire animation, sound, and raycast effect.

avatar image Seth-Bergman · Jul 20, 2012 at 04:33 AM 0
Share

The fire function only gets entered once every .3 seconds, due to the FireRate.. If you move the lines

yield WaitForSeconds(FireRate); CanFire = true;

to the end, and put all your animation, sound, etc. calls above that, It will all only be called once every .3 seconds (or whatever the rate is set to).. isn't "Rate of fire" and "cool down time" the same thing?

avatar image Seth-Bergman · Jul 20, 2012 at 05:10 AM 0
Share

edited my answer above, think that's what you're after..

avatar image Radon · Jul 20, 2012 at 06:22 AM 0
Share

Ok, so now my bullet is arriving late to my target

avatar image Seth-Bergman · Jul 20, 2012 at 06:27 AM 0
Share

already was judging by the old code.. anyway, just delete the line yield WaitForSeconds(FireRate);.. I'll update the code above..

now ins$$anonymous$$d we're using "coolDownRate", but it doesn't matter either way, it's still a float, you could just replace that with FireRate

Show more comments

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

Controlled Automatic Fire Rate (RayCasting) 2 Answers

Start timer with Input 2 Answers

Gun Script glich? 1 Answer

Is there a script out there that actully works for shooting bullets? 2 Answers

Shooting question 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