I know there are many scripts on making ammunition, but I have an odd firing script, and can’t figure out how to implement ammo.
var damage : float = 5;
var gunshot : AudioClip;
var muzzleflash : GameObject;
function Update () {
pistolflash = GameObject.FindWithTag("pistolflash");
// raycast settings
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
var localOffset = transform.position;
// check to see if user has clicked
if(Input.GetButtonDown("Fire1"))
{
// if so, did we hit anything?
if (Physics.Raycast (localOffset, direction, hit, 400))
{
// just so we can see the raycast
Debug.DrawLine (localOffset, hit.point, Color.cyan);
// print to console to see if we have fired
print("we have fired!");
// send damage report to object
hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
AudioSource.PlayClipAtPoint(gunshot,transform.position);
Instantiate (muzzleflash, pistolflash.transform.position, transform.rotation);
}
}
This is for a pistol. I have looked at the FPS tutorial but there is no way i could implement their system. Ultimately, I want to have a clip, ammo, and reload function. Any help is much appreciated.
Have three int variables. One for the current clip, one for clip size, and one for ammo. Before you fire a shot just check if the clip variable is above 0 (at the same time as checking for the fire button).
To reload just have a function which finds the difference between the clipsize variable and the current clip. Then just take that amount away from ammo variable and add it to the current clip.
var thirtyEightBullet : Rigidbody;
var bulletSpeed = 10;
var Magnum : AudioClip;
//var magnumAmmo = 6;
var canFire = true;
var Empty : AudioClip;
var revolverReload : AudioClip;
var isReloading = false;
var canShootAgain = true;
var isActive = false;
var icon : Texture2D;
var reload : Texture2D;
var isPrimary = false;
var magnum_selected : Texture2D;
var magnumMaxAmmo = 6;
var magnumCurrentAmmo = 6;
var shotsFired = 0;
function Shooting() {
if (isPrimary == true)
if (isActive == true)
if (canFire == true)
{
if(canShootAgain == true)
{
if (Input.GetButtonDown("Fire1"))
{
canShootAgain = false;
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(thirtyEightBullet, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(( Vector3(Screen.width / 2,Screen.height / 2 - 50) * bulletSpeed) );
// Give the cloned object an initial velocity along the current
// object's Z axis
// clone.velocity = transform.TransformDirection (Vector3.forward * bulletSpeed);
audio.PlayOneShot(Magnum);
magnumCurrentAmmo -= 1;
shotsFired +=1;
yield WaitForSeconds (1.5);
canShootAgain = true;
if(magnumCurrentAmmo == 0)
{
canFire = false;
}
}
}
}
if (canFire == false && isPrimary == true)
{
if (Input.GetButtonDown("Fire1"))
{
if(isReloading == true)
{
isReloading = true;
}
else
{
audio.PlayOneShot(Empty);
yield WaitForSeconds (0.5);
}
}
}
}
function Reload(){
if(isPrimary == true)
{
if(isActive == true)
{
if(isReloading == false)
{
if(magnumMaxAmmo != 0)
{
if(Input.GetKeyDown("r"))
{
canFire = false;
isReloading = true;
audio.PlayOneShot(revolverReload);
yield WaitForSeconds (5.5);
//no ammo in clip, but more than 6 max
if (magnumCurrentAmmo == 0 && magnumMaxAmmo >= 6)
{
magnumCurrentAmmo = 6;
magnumMaxAmmo = magnumMaxAmmo - shotsFired;
}
//no ammo in clip, but less than 6 max
if (magnumCurrentAmmo == 0 && magnumMaxAmmo < 6)
{
magnumCurrentAmmo = magnumMaxAmmo;
magnumMaxAmmo = 0;
}
//some ammo in clip, but more than 6 max
if (magnumCurrentAmmo < 6 && magnumCurrentAmmo > 1 && magnumMaxAmmo >= 6)
{
magnumCurrentAmmo = 6;
magnumMaxAmmo = magnumMaxAmmo - shotsFired;
}
//some ammo in clip, but less than 6 max
if (magnumCurrentAmmo < 6 && magnumCurrentAmmo > 1 && magnumMaxAmmo < 6)
{
if (magnumCurrentAmmo + magnumMaxAmmo == 6)
{
magnumCurrentAmmo = 6;
magnumMaxAmmo = 0;
}
if ((magnumCurrentAmmo + magnumMaxAmmo) < 6)
{
magnumCurrentAmmo = magnumCurrentAmmo + magnumMaxAmmo;
magnumMaxAmmo = 0;
}
if ((magnumCurrentAmmo + magnumMaxAmmo) > 6)
{
magnumCurrentAmmo = 6;
magnumMaxAmmo = magnumMaxAmmo - shotsFired;
}
}
shotsFired = 0;
if(magnumCurrentAmmo == 0 && magnumMaxAmmo == 0)
{
canFire = false;
}
shotsFired = 0;
canFire = true;
isReloading = false;
}
}
}
}
}
}
function OnGUI()
{
GUI.Label (Rect (Screen.width / 2 , Screen.height - 20,150,100), "" + magnumCurrentAmmo + "/" + magnumMaxAmmo);
if (isPrimary == true)
{
GUI.Box (Rect (Screen.width / 2, Screen.height - 50 ,170,50), icon);
}
else
{
GUI.contentColor = Color.gray;
GUI.Box (Rect (Screen.width / 2, Screen.height - 50 ,170, 50), icon);
}
if (magnumCurrentAmmo == 0 && magnumMaxAmmo == 0)
{
GUI.contentColor = Color.red;
GUI.Label (Rect (Screen.width / 2, Screen.height - 20,150,100), "" + magnumCurrentAmmo + "/" + magnumMaxAmmo);
}
if (magnumCurrentAmmo == 0 && isPrimary == true && isActive == true)
{
if (Time.time % 1.1 < 1)
GUI.Label (Rect (Screen.width / 2 - 75, Screen.height / 4, 200, 50), reload);
}
}
function Update () {
if (Input.GetKey("4"))
{
isActive = true;
isPrimary = true;
}
else if(Input.GetKey("1") || Input.GetKey("2") || Input.GetKey("3") || Input.GetKey("5") || Input.GetKey("6"))
{
isActive = false;
isPrimary = false;
}
Shooting();
Reload();
}
I know this is long, but it’s everything you need.
This is only the reload function, and I hope you find it useful:
function Reload(){
if(Ammo > 0 && MAG < 30) // MAG is the gun magazine or bullets in the gun.
{
animation.Play(“reload”);
Ammo = MAG + Ammo;
if(Ammo >= 30)
{
MAG = 30;
Ammo = Ammo - 30;
}
else
{
MAG = Ammo;
Ammo = 0;
animation.Play("reload");
}
}
}
public int augAmmo = 30;
public int augAmmoDif;
public int augAmmoMax = 900;
public int augClip = 30;
public int augClipSize = 30;
public int augClipDif;
void Reload()
{
if (augAmmo >= 1 && augClip <= 30)
{
if (Input.GetButtonDown(“Reload”))
{
augAnim.SetBool(“Reloading”, true);
augSource.PlayOneShot(augReload, 1f);
}
}
}
public void reloaded()
{
augAnim.SetBool("Reloading", false);
if (augAmmo <= augClipSize)
{
augClipDif = (augClipSize - augClip);
if(augAmmo <= augClipDif)
{
augAmmoDif = (augClipDif - augAmmo);
augClip += augAmmoDif;
augAmmo = 0;
}
else
{
augClip += augClipDif;
augAmmo -= augClipDif;
}
}
else
{
augClipDif = (augClipSize - augClip);
augAmmo = (augAmmo - augClipDif);
augClip = augClipSize;
}
if (augAmmo <= 0)
{
augAmmo = 0;
}
}