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 Dragonfly3r · Apr 07, 2015 at 04:15 AM · c#raycastgunreloadgunfire

Reload clips on Gun-script not applied properly

I'm trying to make my gun script reload similar to that of current FPS in which if a player shoots 1 bullet and the player reloads the gun then it will reload the gun to the full clip amount and take into account of the 1 ammo left so that on the final clip of ammo there'd be 1 ammo less than on a normal clip.

For example you have 35 ammo with 7 per clip. You shoot say 2 times and then reload meaning your total ammo is 33. You will have a full clip until your last clip where there will be 5 ammo in the clip instead of the full 7 due to the already spent ammo.

I've managed to make it so my gun reloads after all 7 ammo per clip has been used up and then reloads with another 7 ammo however I have been unable to figure out how to implement the above problem. Anyone able to help me out to figuring out what I need to do, to do this?

RayShoot Script

 public int clip = 80;
 public int bulletsPerClip = 60;
 public int bulletsLeft = 0;
 public int particleSpeed = 200000;

 public float damage = 10.0f;
 public float range = 1000.0f;
 public float reloadTime = 1.6f;
 public float force = 1000;
 public float fireSpeed = 15.0f;
 
 [HideInInspector]
 public float waitTillFire = 0;
 
 public GameObject bullet;
 public GameObject bulletSpawn;
 
 public float shootTimer = 0.0f;
 public float shootCooler = 0.1f;
 
 public float muzzleFlashCooler = 0.1f;
 public float muzzleFlashTimer = 0.0f;

 public float keyCooler = 0.5f;
 public float keyTimer = 0.0f;

 // public GameObject light1;
 // public GameObject light2;
 //  public GameObject light3;

 public AudioClip reloadSound;
 public AudioClip shootSound;
 public AudioClip emptyGunSound;

 public ParticleEmitter muzzleFlash;
 // public ParticleEmitter hitParticles;

 
 // Use this for initialization
 void Start () 
 {
     bulletsLeft = bulletsPerClip;
   //  hitParticles.emit = false;
     muzzleFlash.gameObject.SetActive(true);
     muzzleFlash.emit = false;

 }
 
 // Update is called once per frame
 void Update () 
     {
     if (keyTimer > 0)
     {
         keyTimer -= Time.deltaTime;
     }

     if (keyTimer < 0)
     {
         keyTimer = 0;
     }

     if (muzzleFlashTimer > 0)
     {
         muzzleFlashTimer -= Time.deltaTime;
         MuzzleFlashShow();
     }

     if (muzzleFlashTimer < 0)
     {
         muzzleFlashTimer = 0;
     }

     if (shootTimer > 0)
     {
         shootTimer -= Time.deltaTime;
     }

     if (shootTimer < 0)
     {
         shootTimer = 0;
     }

     if (keyTimer == 0)
     {
         if (Input.GetMouseButton (0) && bulletsLeft > 0)
         {
             if (shootTimer == 0)
             {
                 PlayShootAudio();
                 RayFire();
                 Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
                 shootTimer = shootCooler;
                 keyTimer = keyCooler;
             }

             if (muzzleFlashTimer == 0)
             {
                 muzzleFlashTimer = muzzleFlashCooler;
                 MuzzleFlashShow();
             }
         }
         else if (Input.GetMouseButtonDown (0) && clip == 0)
         {
             PlayEmptyAudio();
             shootTimer = shootCooler;
             keyTimer = keyCooler;
         }
         if (Input.GetKey(KeyCode.R))
         {
             Reload();
             shootTimer = shootCooler;
             keyTimer = keyCooler;
         }
     }
 }

 void MuzzleFlashShow()
 {
     if (muzzleFlashTimer > 0)
     {
         muzzleFlash.emit = false;
        // light1.SetActive(false);
       //  light2.SetActive(false);
       //  light3.SetActive(false);
     }

     if (muzzleFlashTimer == muzzleFlashCooler)
     {
         muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360 * particleSpeed, Vector3.forward);
         muzzleFlash.emit = true;
      //   light1.SetActive(true);
      //   light2.SetActive(true);
      //   light3.SetActive(true);
     }
 }

 void RayFire()
 {
     GameObject.Find("CQAssaultRifle").animation.Play("GunFiring"); // First name is for name of object that will play your animation and second name is what you animation is called

     RaycastHit hit;
     Vector3 directionRay = transform.TransformDirection(Vector3.forward);
     Debug.DrawRay(transform.position, directionRay * range, Color.yellow);
     
     if (Physics.Raycast(transform.position, directionRay, out hit, range))
     {
         if (hit.rigidbody)
         {
             // if (hitParticles)
             // {
             //      hitParticles.transform.position = hit.point;
             //      hitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);
             //      hitParticles.Emit();

             hit.rigidbody.AddForceAtPosition(directionRay * force, hit.point);

             //      hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
             // }
         } 
     }
     
     bulletsLeft--;

     if (bulletsLeft < 0)
     {
         bulletsLeft = 0;
     }        
     if (bulletsLeft == 0)
     {
         Reload();
     }            
 }
 void PlayShootAudio()
 {
     audio.PlayOneShot(shootSound);
 }

 void PlayReloadAudio()
 {        
     audio.PlayOneShot(reloadSound);
 }

 void PlayEmptyAudio()
 {
     audio.PlayOneShot(emptyGunSound);
 }

 void Reload()
 {
     if (clip > 0)
     {
         StartCoroutine(ReloadSpeed());
         clip--;
     }        
 }

 IEnumerator ReloadSpeed()
 {
     if (clip > 0)
     {
         // GameObject.Find("").animation.Play(""); // First name is for name of object that will play your animation and second name is what you animation is called
         PlayReloadAudio();
         yield return new WaitForSeconds(reloadTime);

         //if (clip > 0)
         //{
         bulletsLeft = bulletsPerClip;
         //}
     }
     else if (clip < 0)
     {
         PlayEmptyAudio();
     }
 }   
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
0

Answer by ppoomi · Apr 07, 2015 at 12:13 PM

if (clip > 0) { StartCoroutine(ReloadSpeed()); clip--;

www.pass4sure.co.uk/CCNP-training.html

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 Dragonfly3r · Apr 09, 2015 at 02:05 AM 0
Share

That doesn't tell me anything. All you did was just write a section of my code and didn't tell me what to do with it bar just advertising software to buy.

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

How can i make a ray cast take health from enemies 2 Answers

Raycast Projectile w/ Physics 2 Answers

realistic reload system? 1 Answer

Make bullet launch at center of screen 2 Answers

Need Help With a Gun Reloaded script pls help? 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