Hello, I am still rather new to scripting and for my shooting raycast script I have tried to add a reload mechanic into it. So far the magazine works and removes a bullet every shot, however my reload component of the script is not working, with no errors or anything showing to indicate what the issue is. Any help is appreciated, thanks.
Here is my script:
var bulletTex : GameObject[ ];
var power : int = 10;
var fireRate : float = 0.5;
private var nextFire : float = 0.0;
var gunShot : AudioClip;
var totalAmmo = 90;
var bullets = 30;
var reloadTime = 3;
var reloadAmount = 30;
var reloadSound: AudioClip;
var ammo : GUIText;
function Update () {
Fire ();
}
function Fire () {
if(Input.GetButton(“Fire1”) && Time.time > nextFire && bullets > 0) {
nextFire = Time.time + fireRate;
AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
ForceFire();
bullets-=1;
}
}
function ForceFire () {
var fwd = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd * 10, Color.green);
if(Input.GetButton(“Fire1”) && Physics.Raycast(transform.position, fwd, hit, 10)&& bullets > 0) {
Instantiate(bulletTex[Random.Range(0,1)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
if (hit.rigidbody !=null)
hit.rigidbody.AddForceAtPosition(fwd * power, hit.point);
}
}
function Reload (){
if(bullets < 1){
if(Input.GetKeyDown(“r”)){
AudioSource.PlayClipAtPoint(reloadSound, transform.position, 1);
yield WaitForSeconds(reloadTime);
bullets += reloadAmount;
totalAmmo -= reloadAmount;
}
}
}
function OnGUI () {
ammo.text = “” + bullets + “/” + totalAmmo.ToString();
}