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 19, 2015 at 10:37 PM · c#gunammoreloadingfiring

How do I get Single ammo reloading to work?

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?

I've implemented below the use of the R button on the keyboard for when you will reload for single shot ammo however when I currently reload it just slows the audio for reloading so it sounds all distorted. And also when bulletsLeft is = to less than 0 in the clip then it will automatically reload a fresh new clip of 7 ammo.

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 keyCooler = 0.5f;
  public float keyTimer = 0.0f;
  
  // Use this for initialization
  void Start () 
  {
  bulletsLeft = bulletsPerClip;
  //  hitParticles.emit = false;
 
  }
 
  // Update is called once per frame
  void Update () 
  {
  if (keyTimer > 0)
  {
      keyTimer -= Time.deltaTime;
  }
 
  if (keyTimer < 0)
  {
      keyTimer = 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;
          }
      }
      else if (Input.GetMouseButtonDown (0) && clip == 0)
      {
          PlayEmptyAudio();
          shootTimer = shootCooler;
          keyTimer = keyCooler;
      }
      if (Input.GetKey(KeyCode.R))
      {
          Reload();
          shootTimer = shootCooler;
          keyTimer = keyCooler;
      }
  }
  }
 
 
  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 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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

having trouble with ammo reload script. destorys extra ammo 1 Answer

Gun script not working 3 Answers

FPS UI ammo display problem 0 Answers

how to access a specific instance of a script 1 Answer

Multiple Cars not working 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