Skip to main content

Simple Reload Script

Sooo I'm working on a simple Top Down Shooter game. And This was the script I came up with for the reloading part of the script. Very simple. I think you can use it with any weapon really. You reload with space. You  need to put your own method of Firing in this script. Comment if you have any questions



private var reloading = false;
var reloadTime = 2.0;
private var st : int;

var clipAmmo = 50;
var clipSize = 50;
var stockAmmo = 100;



function Update () {


if (Input.GetKeyDown ("space")){
if(clipAmmo<clipSize && reloading == false && stockAmmo !=0){
SaveTime(Time.time);
reloading = true;
}
}


if(st < Time.time && reloading == true){
Reload();
}

}

//Making it show that you are reloading.
function OnGUI(){

if(reloading == true){
GUI.Box(Rect(250,200,100,25),"Reloading");
}
}


function Reload(){
if(clipAmmo < clipSize){
var neededAmmo = clipSize-clipAmmo;
if(stockAmmo >neededAmmo){
clipAmmo = clipSize;
stockAmmo -= neededAmmo;
}else if(stockAmmo <= neededAmmo){
clipAmmo += stockAmmo;
stockAmmo = 0;
}
}
reloading = false;
}

function SaveTime(time){
st = reloadTime + time;
}



Comments

  1. Good! Had issues with mine for some reason.

    ReplyDelete
  2. I have a error of the line st = reloadTime + time; saying Operator '+' cannot be used with a left hand side of type 'float' and a right hand side of type 'Object'.

    ReplyDelete
    Replies
    1. That's intresting. I haven't worked in Javascript code in quiet a while now. Looking back at this code I see now that the whole function was pointless. Replace where SaveTime is being called with the code:

      st = reloadTime + Time.time;

      Delete

Post a Comment