• 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
Question by Blink · Jul 14, 2012 at 09:52 AM · animationreloadingmuzzleflash

Muzzle flash stop when reloading?

Hi all, I have wrote this script that spawns a simple particle effect of a muzzle flash at the end of my gun when the LMB is clicked, I have other scripts on my gun that control shooting raycasts and reloading ect, but I'm having a problem with how I would stop the muzzle flash for being able to be spawned when the gun is reloading, I have done so with my raycasts and animations but I don't know how I would do it for this, can anyone help me out please?

 var muzzleFlash : GameObject;

var spawnPoint : Transform;

function Update () {

if(Input.GetButtonDown("Fire1")){ Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation); } }

Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by FLASHDENMARK · Jul 16, 2012 at 03:56 PM

As I said. You should add a reload variable to keep track of if you are reloading or not. Here is something to help you along the way:

 var muzzleFlash : GameObject; 
         var spawnPoint : Transform; 
         var amountOfShots = 8; 
         var reloadTime = 1.5;
     
         private var reloading = false;
         
     function Update (){
         if(Input.GetButtonDown("Fire1") && !reloading){ //Check for input and that we aren't reloading
     
         //Exclamation mark pretty much means "is false"
              Shoot(); 
             }
         }
         
         if(Input.GetKeyDown("r")){ 
             Reload(); 
     }
         
     function Reload (){
         reloading = true; //Set reloading to true because... We are reloading
         
         yield WaitForSeconds(reloadTime); 
     
         amountOfShots = 8; 
         reloading = false; //Set reloading to false because we are done reloading
     }
         
      function Shoot (){
             //All of this below will now only execute when we are not reloading
         if(amountOfShots > 0){ //If we have ammunition left
             amountOfShots--;
         
             Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation);
         }
     }

Not tested, but might work and you should be able to get an idea of what to do.

Comment

People who like this

0 Show 0 · 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

Answer by programmrzinc · Jul 14, 2012 at 09:49 PM

 var muzzleFlash : GameObject;
 var spawnPoint : Transform;
 
 function Update () {
 
 if(Input.GetButtonDown("Fire1")){
 Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation);
  } 
 if(Input.GetButtonUp("Fire1")){
 Destroy(muzzleFlash);
  }
 }

this is so when the key comes up, the gameobject will destroy

Comment

People who like this

0 Show 3 · 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 Blink · Jul 15, 2012 at 09:38 AM 0
Share

my particle effect already does this, what I meant was when my gun runs out of ammo (8) it cant be spawned even if the LMB has been clicked and then be able to spawn again once I have reloaded.

avatar image FLASHDENMARK · Jul 15, 2012 at 09:41 AM 0
Share

Just add a reloading variable. When reloading set it to true and make it so that you can only spawn your muzzle flash when reloading is false.

avatar image Blink · Jul 16, 2012 at 03:34 PM 0
Share

I have altered it to this but it doesn't work and I don't know what to do, can you help me out? sorry for the bad formatting.

var muzzleFlash : GameObject; var spawnPoint : Transform; var amountOfShots = 8; var reloadTime = 1.5;

function Update () {

if(Input.GetButtonDown("Fire1")){ Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation); Shoot(); } }

if(Input.GetKeyDown("r")){ Reload(); }

function Reload () {

yield WaitForSeconds(reloadTime); amountOfShots = 8; }

function Shoot (){

     if(amountOfShots > 0){
     amountOfShots--;

} }

avatar image

Answer by Blink · Jul 17, 2012 at 03:52 PM

I've figured it out now, I have posted the script below thanks for the help and directions!!

 var muzzleFlash : GameObject;

var spawnPoint : Transform;

var fullClip : int = 8;

var bulletsLeft : int = 8;

function Update () {

if(Input.GetButtonDown("Fire1")){

if(bulletsLeft > 0 ){

bulletsLeft -- ;

Instantiate(muzzleFlash, spawnPoint.position, spawnPoint.rotation);

}

}

if(Input.GetButtonDown("r")){

bulletsLeft = fullClip ;

}

}

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Stop animation when no more bullets 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity Invoke method delay factor vary device to device 1 Answer

Trouble getting a trigger to activate an animation 2 Answers

iTween Get the path from object nodes and move the object at same speed by loop 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