# 1911 script

**URL:** https://discussions.unity.com/t/1911-script/529105
**Category:** Scripting
**Tags:** Scripting
**Created:** [February 16, 2014, 10:06pm UTC](https://discussions.unity.com/t/1911-script/529105 "2014-02-16T22:06:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kelkarn](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@kelkarn](https://discussions.unity.com/u/kelkarn)
#### Post date: [February 16, 2014, 10:06pm UTC](https://discussions.unity.com/t/1911-script/529105/1 "2014-02-16T22:06:14Z")

</div>

can someone help me with this script I want to make it so the if I hit the r key it plays my reload animation and refills the gun  
var bullet : Rigidbody;  
var power : float = 1500;  
var damage : float = 100;  
var reloadtime : float = 4; // this reload time is too long!  
var magcount : int = 10;  
var magbulletcount : int = 21;  
var bulletcount : int = 0;  
var Reload : AnimationClip;  
var Idel : AnimationClip;  
var AutoFire : AnimationClip;  
var ReloadAudio : AudioClip;  
var AutoFireAudio : AudioClip;  
var CockAudio : AudioClip;

private var reloadTimer: float = 0.0; // internal reload timer

function Start () {  
bulletcount = magbulletcount;  
}

function Update(){  
if (reloadTimer \> 0){ // if reloadTimer active…  
reloadTimer -= Time.deltaTime; // decrement it  
if (reloadTimer \<= 0){ // if reloadTime ended…  
bulletcount = magbulletcount; // load a full clip…  
magcount–; // and decrement clip count  
}  
}  
else // only shoot when reloadTimer not active  
if (Input.GetButtonDown(“Fire1”) bulletcount \> 0){  
animation.Play(“AutoFire\_1911”);  
audio.PlayOneShot(AutoFireAudio);

var instance: Rigidbody = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;  
var fwd: Vector3 = transform.TransformDirection(Vector3.forward);  
instance.AddForce(fwd \* power);  
bulletcount --;  
if (bulletcount \<= 0 magcount \> 0){

// if run out of ammo but still have clips…  
reloadTimer = reloadtime; // activate reloadTimer  
// you can play reload sound or animation here  
animation.Play(“Reload\_1911”);  
audio.PlayOneShot(ReloadAudio);  
audio.PlayOneShot(CockAudio);  
}

}  
}

---

<div class="post-metadata">

### Author: ![Sydnethy](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@Sydnethy](https://discussions.unity.com/u/Sydnethy)
#### Post date: [February 17, 2014, 2:04am UTC](https://discussions.unity.com/t/1911-script/529105/2 "2014-02-17T02:04:03Z")

</div>

Here is pseudo-code. You should pick up on the general idea.

```auto
var magazineCapacity : int = (Some number);
var currentAmmo : int = (Some number);

var reloadAnim : AnimationClip;

if (Input.GetKeyDown("r")){

    currentAmmo = magazineCapacity;

    animation.Play("reloadAnim");
}

```

Edit:

To add a reload time you should check this out: [Unity - Scripting API: WaitForSeconds](http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds.html)

---

<div class="post-metadata">

### Author: ![CaptainScience\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/captainscience_1/32/450841_2.png) [@CaptainScience\_1](https://discussions.unity.com/u/CaptainScience_1)
#### Post date: [February 17, 2014, 2:05am UTC](https://discussions.unity.com/t/1911-script/529105/3 "2014-02-17T02:05:11Z")

</div>

No one is going to write your script for you. If you have a specific difficulty, you can post the script using the “CODE” tags and someone can probably give you some advice.

Arcade-style gun reloading scripts are easy. Just add rounds to the gun and subtract from your total. More realistic behaviour with a slide catch, hammer cocking-decocking, chamber full/empty, magazine capacities, etc. is a bit more complicated, but it is nothing that can’t be solved with basic programming skills.
