• 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 DGArtistsInc · Jan 29, 2012 at 12:18 AM · fps

Machine Gun Problems

Old issue: Machine gun stays disabled even when i press the fire1 button and doesn't shoot. Any help is appreciated.

So now here is the new issue... I can fire the first round however i can't fire after the first round because according to the inspector view it is not reloading. So here is my script

 using UnityEngine;
 using System.Collections;

 public class MachineGun : MonoBehaviour {

 public float range = 100.0F;
 public float fireRate = 0.05F;
 public float force = 10.0F;
 public float damage = 5.0F;
 public int bulletsPerClip = 40;
 public int clips = 20;
 public float reloadTime = 0.5F;
 public Renderer muzzleFlash;
 
 private int bulletsLeft = 0;
 private float nextFireTime = 0.0F;
 private int m_LastFrameShot = -1;
 
 void Start () {
     bulletsLeft = bulletsPerClip;
 }
 
 void LateUpdate()
 {
     if (muzzleFlash)
     {
         if (m_LastFrameShot == Time.frameCount)
         {
             muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.Range(0, 359), Vector3.forward);
             muzzleFlash.enabled = true;
             
             if (audio) {
                 if (!audio.isPlaying)
                     audio.Play();
                 audio.loop = true;
             }    
         }
         else
         {
             muzzleFlash.enabled = false;
             
             enabled = false;
             if (audio)
             {
                 audio.loop = false;
             }
         }
     }
 }
 
 void Fire()
 {
     if(bulletsLeft == 0)
         return;
     if(Time.time - fireRate > nextFireTime)
         nextFireTime = Time.time - Time.deltaTime;
     while(nextFireTime < Time.time && bulletsLeft != 0)
     {
         FireOneShot();
         nextFireTime += fireRate;
     }
 }
 
 void FireOneShot()
 {
     Vector3 direction = transform.TransformDirection(Vector3.forward);
     RaycastHit hit;
     
     if(Physics.Raycast(transform.position, direction, out hit, range))
     {
         if(hit.rigidbody)
             hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
             
         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
     }
     
     bulletsLeft--;
     
     m_LastFrameShot = Time.frameCount;
     enabled = true;
     
     if(bulletsLeft >= 0)
         Reload();
 }
 
 IEnumerator Reload()
 {
     yield return new WaitForSeconds(reloadTime);
     
     if(clips > 0)
     {
         clips --;
         bulletsLeft = bulletsPerClip;
     }
 }
 
 int GetBulletsLeft()
 {
     return bulletsLeft;
 }
 }

so there is no errors and now i have a BroadcastMessage("Fire") on another script so thats not a problem anymore... so any help is appreciated.

Comment
Add comment · Show 1
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 perchik · Jan 29, 2012 at 12:53 AM 0
Share

If it was me, I'd put in a Debug.Log statement in to make sure it's actually seeing fire1 get pressed. Then I'd put a debug statement at the beginning of FireOneShot() to see if it's getting there.

1 Reply

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

Answer by flamy · Jan 29, 2012 at 06:21 AM

the reason y it doesnt turn on back is because you are disabling the GameObject and trying to enable it from the same script, which is not possible because the scripts in disabled gameObject will never be called unless some other script in some other gameObject makes enabled to true..

so have an instance of both muzzleFlash and the script above in someother script. Usually enabling and disabling should be done by some generic script that will be active through out the scene(just a suggestion).

Comment
Add comment · Show 7 · 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 DGArtistsInc · Jan 29, 2012 at 09:50 PM 0
Share

Oh ok i see what you are saying i know the perfect way to fix it Thanks!

avatar image DGArtistsInc · Feb 04, 2012 at 03:58 PM 0
Share

ok sorry but i now have a new problem Updated Question ^

avatar image perchik · Feb 04, 2012 at 04:46 PM 0
Share

I don't know if I speak for the community or not, but I think it's an absolutely terrible idea to completely change a question like this...If someone has your original problem they'll never be able to find it now. Please just post a new question and link to the old one if it's related

avatar image DGArtistsInc · Feb 04, 2012 at 05:17 PM 0
Share

well first it is a pain to ask questions on this site and make them perfect grammar and code wise(because if they aren't people complain). And in addition NOW the question basically describes my previous question if that makes you happy. SO if you can please answer my question

avatar image DGArtistsInc · Feb 05, 2012 at 06:07 PM 1
Share

never $$anonymous$$d i figured it out. I needed to start the coroutine and switch up one of my inequalities. Thanks for the help

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

Basic scripting help (FPS Tutorial) 1 Answer

Shooting script problem 0 Answers

Shooting Script Help 1 Answer

(flags & CollisionFlags.CollidedBelow) != 0; ??? 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