Duplicate
Need help with shooting and reload script
Hello, I have a shooting script that Instantiates bullets when the mouse0 button is pressed also it sends a ray cast to make bullet holes when the ray cast hits the walls. This is good but now I want to add a reload function ,I have an idea of how to do this but I don't really know how to put it into code (I'm not a beginner).
I would also like it to have a reload animation. thanks :)
using UnityEngine; using System.Collections;
public class Shoot : MonoBehaviour {
 public GameObject bullet;
 public GameObject bulletHole;
 public float delayTime = 2f;
 private float counter = 0f;
 void Update () 
 {
 }
 
 void FixedUpdate () 
 {
     if (Input.GetKey (KeyCode.Mouse0) && counter > delayTime) 
     {
         Instantiate(bullet, transform.position, transform.rotation);
         GetComponent<AudioSource>().Play();
         counter = 0;
     
         RaycastHit hit;
         Ray ray = new Ray(transform.position, transform.forward);
         if (Physics.Raycast(ray, out hit, 70f))
         {
             Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
         }
     }
     counter += Time.deltaTime;
 }
}
Im not sure if this is needed but here is the script for the bullet.
using UnityEngine; using System.Collections;
public class MoveBullet : MonoBehaviour { public float speed = 1f;
 void Start () 
 {
 
 }
 
 void Update () 
 {
     transform.Translate (0, 0, speed);
 }
}
Also it would be nice if I can have ammo added to it as well thanks :)
there is something you didn't specify, do you fire one bullet then reload then one bullet ect... or do you fire x bullet then reload then fire x bullet, ...?
Sounds suspiciously like you are requesting we do it for you.
There are seriously THOUSANDS of answers and scripts on reloading a weapon. Please search and absorb some of the knowledge. If you are not a beginner you should be doing this yourself.
Search first, try something (many things), ask for help. In that order please.
@Fire_Cube please dont encourage script requests. We don't code to spec.
Answer by LynoHD · Nov 03, 2015 at 10:42 AM
Im not exactly sure how to do reloading correctly, but me personally, i do it in a corutine.
and this is how i would go about doing that
 public float reloadTimer = 3f; //3 seconds
 public bool isReloading = false;
 public int bullets = 30; //bullets in weapon
 public int bulletsToReload = 30; //bullets to add when reloading
 
 public int magazines = 5; //magazines left.
 Void Update()
 {
 if(Input.getKeyDown(Keycode.R) && !isReloading) //check if pressed reload key. and check if you're not already doing a reload.
 {
 if(magazines <= 0) return; //stop if you do not have enough magazines
 StartCorutine(Reload()); //starts the Reload Corutine <- might have spelled corutine wrong.
 }
 }
 
 public IEnumerator Reload()
 {
 isReloading = true;
 yield return new WaitForSeconds(reloadTimer);
 //add the ammo
 //play the animations, etc etc
 bullets = bulletsToReload;
 magazines--;
 isReloading = false;
 
 }
Follow this Question
Related Questions
Gun not reloading properly & time not deducting correctly when below 10 seconds 2 Answers
Basic Unity: Can someone explain the referencing and instantiation process? 1 Answer
Instantiate a script into an instantiated prefab 3 Answers
Reloads correctly first time, but can't reload till count hits 0 1 Answer
How to get a public int equal the same number when added in all object that have the same script? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                