How do I make a gun reload after 30 shots?
Ok, Im currently making a FPS game which involves animations with reloading, firing, and drawing a weapon. I need to make it so that my weapon stops and reloads after shooting 30 bullets. I also want the weapon to stop shooting after all of the clips run out (total of 3). I need help on making this. The script I'm using is below.
var drawAnim : String = "Draw";
var fireLeftAnim : String = "Fire";
var reloadAnim : String = "Reload";
var animationGO : GameObject;
private var drawWeapon : boolean = false;
private var reloading : boolean = false;
function Start (){
DrawWeapon();
}
function Update (){
if(Input.GetButton ("Fire1") && reloading == false && drawWeapon == false){
Fire();
}
if (Input.GetKeyDown ("r") && reloading == false && drawWeapon == false){
Reloading();
}
if (Input.GetKeyDown ("1") && reloading == false){
DrawWeapon();
}
}
function Fire(){
animationGO.GetComponent.<Animation>().CrossFadeQueued(fireLeftAnim, 0.08, QueueMode.PlayNow);
}
function DrawWeapon() {
if(drawWeapon)
return;
animationGO.GetComponent.<Animation>().Play(drawAnim);
drawWeapon = true;
yield WaitForSeconds(0.6);
drawWeapon = false;
}
function Reloading(){
if(reloading) return;
animationGO.GetComponent.<Animation>().Play(reloadAnim);
reloading = true;
yield WaitForSeconds(2.0);
reloading = false;
}
It worked here !
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Gun$$anonymous$$anager : $$anonymous$$onoBehaviour {
[Header("Tiro")]
//Lançamento das balas//
public Rigidbody Projetil;
public Transform SpawnBullet;
public float VelocidadeDaBala = 20;
//Lançamento das balas//
[Header("$$anonymous$$ira")]
//$$anonymous$$ira//
public Transform GunPrefab;
public GameObject miraCanvas;
public Vector3 Aim;
public Vector3 Hip;
//$$anonymous$$ira//
//Reload//
[Header("Reload")]
public float TempoDeReload;
public int BalasNoPente = 30;
int balastart;
public int BalasForaDoPente = 90;
int bulletfor;
bool reload;
//Reload//
void Start () {
BalasForaDoPente = 90;
bulletfor = 0;
balastart = BalasNoPente;
}
void Update () {
//========Lançador_SE$$anonymous$$I_AUTO$$anonymous$$ATICO========//
if (BalasNoPente > 0) {
if (Input.GetButtonDown ("Fire1")) {
BalasNoPente --;
bulletfor++;
Rigidbody instantiateProjetil = Instantiate (Projetil, SpawnBullet.position, SpawnBullet.rotation) as Rigidbody;
instantiateProjetil.velocity = transform.TransformDirection (new Vector3 (0, 0, VelocidadeDaBala));
}
}
//========Lançador========//
//========$$anonymous$$ira========//
if (Input.Get$$anonymous$$ouseButton (1)) {
GunPrefab.localPosition = Aim;
miraCanvas.SetActive (false);
}
if (Input.Get$$anonymous$$ouseButtonUp (1)) {
GunPrefab.localPosition = Hip;
miraCanvas.SetActive (true);
}
//=====Reload======//
if (BalasNoPente < balastart) {
reload = true;
} else {
reload = false;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R) && reload == true) {
StartCoroutine(Reload());
}
}
IEnumerator Reload(){
yield return new WaitForSeconds (TempoDeReload);
BalasNoPente += bulletfor;
BalasForaDoPente -= bulletfor;
bulletfor = 0;
}
//=====Reload======//
}
Answer by ItsIcear · Feb 10, 2016 at 05:39 PM
First, create an int that keeps track of your current ammo count and a bool
var ammoCount : int = 30; // Use Whatever Value You Like
var emptyMagazine : bool = false;
Then when you call the "Fire" function, decrement the "ammoCount" value by 1
ammoCount--;
In your Update function or in your fire function, add the lines
if(ammoCount <= 0)
{
Debug.Log("No ammo left in magazine");
emptyMagazine = true;
}
if(Input.GetKeyDown(KeyCode.R) && emptyMagazine)
{
Reload();
}
Also change you Fire calling to
if(Input.GetButton ("Fire1") && !reloading && !drawWeapon && !emptyMagazine)
{
Fire();
}
// The "!drawWeapon" function does the same as "drawWeapon == false"
Then in your Reload function
ammoCount = 30; // Or Whatever Value You Selected Up Top
emptyMagazine = false;
This should count the number of bullets you have left in the magazine, which could be useful when making a HUD. Also the gun will not fire if you have no bullets left in the magazine. Hope this helps and good luck with you game!
I just finished creating a shoot script that will clone a bullet and launch it using instantiate and ray cast. How do I add the shoot script to my weapon script (the one earlier) so that the bullets would stop cloning and shooting after the 30 shots run out? Ive tried it before but something went wrong. (script below)
#pragma strict
var bulletTex : GameObject[];
var power : int = 10;
function Start ()
{
}
function Update ()
{
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd * 10, Color.green);
if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)){
Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
if (hit.rigidbody !=null)
hit.rigidbody.AddForceAtPosition(fwd * power, hit.point);
}
}
When instantiating the bullet prefab, use the "Destroy" function, like this...
var timeUntilDestroy : float = 5f;
Destroy((prefab), timeUntilDestroy);
Answer by rajan4uto · Feb 10, 2016 at 11:58 AM
in your fire method put a counter and check every time whether it reach 30 or not. For Example:
function Fire()
{
count--;
if(count==0)
{
//reload your gun(use your own method here)
count=30;
}
}
Answer by RealSoftGames · Feb 10, 2016 at 12:08 PM
ok so we need to modify your code a fair bit here, you dont have anything that controls ammo, so lets add this
int ammo = 30, stock ammo = 90; // ammo is what will be in your gun and stock ammo will be the stock pile you have in terms of clips etc.
in your fire method
in
function Fire(){
if(ammo > 0){
animationGO.GetComponent.<Animation>().CrossFadeQueued(fireLeftAnim, 0.08, QueueMode.PlayNow);
//add
ammo--; //this will control the ammo in the gun making it reduce everytime you fire
}
else
//reload here if your ammo is not greater than 0
}