- Home /
Weapon Reload Problem...
Hello. I have a problem. My code is working, weapon is shooting well, i haven't got any errors.But reloading isn't working.Sorry for my English, I'm from Czech republic. Here is my code:
var Bullet : Transform;
var Spawn : Transform;
var shot : AudioClip;
var fireRate : float = 1.0;
private var nextFire : float = 0.0;
private var t : Transform;
var isReloading = false;
var reloadTime = 0.5;
var ammoCount = 20;
var ammoInMagazine = 20;
var clips = 5.0;
var currentBullets = 20;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.)
function Start () {
t = transform;
}
function Update () {
if (Input.GetButton("Fire1") && IsOkToShoot())
Shoot();
}
function IsOkToShoot () : boolean {
var itsOk : boolean = false;
if (Time.time>nextFire) {
nextFire = Time.time + fireRate;
itsOk = true;
}
return itsOk;
}
function Shoot()
{
if (currentBullets > 0) //only shoot if you have ammo.
var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
pel.rigidbody.AddForce(transform.forward * 20000);
AudioSource.PlayClipAtPoint(shot, t.position);
currentBullets--; //subtract one
}
if (currentBullets <= 0){
Reload();
}
function Reload(){
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0) {
clips--;
currentBullets = ammoInMagazine;
}
}
Answer by Landern · Jun 20, 2014 at 01:05 PM
If this is a copy and paste of your actual script, you have incomplete blocks of code (the curly braces). See below.
var Bullet : Transform;
var Spawn : Transform;
var shot : AudioClip;
var fireRate : float = 1.0;
private var nextFire : float = 0.0;
private var t : Transform;
var isReloading = false;
var reloadTime = 0.5;
var ammoCount = 20;
var ammoInMagazine = 20;
var clips = 5.0;
var currentBullets = 20;//This is the amount RIGHT NOW that the gun has. (should be 0 by default.)
function Start () {
t = transform;
}
function Update () {
if (Input.GetButton("Fire1") && IsOkToShoot())
Shoot();
}
function IsOkToShoot () : boolean {
var itsOk : boolean = false;
if (Time.time>nextFire) {
nextFire = Time.time + fireRate;
itsOk = true;
}
return itsOk;
}
function Shoot() {
//only shoot if you have ammo.
if (currentBullets > 0) { // <-- Need curly or only one line of code is invoked.
var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
pel.rigidbody.AddForce(transform.forward * 20000);
AudioSource.PlayClipAtPoint(shot, t.position);
currentBullets--; //subtract one
}
//} // <-- that would close the shoot function previously.
if (currentBullets <= 0){
Reload();
}
}
function Reload() {
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0) {
clips--;
currentBullets = ammoInMagazine;
}
}
@$$anonymous$$ury05 if this helped you then you should select this as your answer by clicking the tick mark next to this answer by @Landern. It is the best way to say thank you to the answerer. :-)
Next time take care of your indent Style! make it easier for you and other programmers to read >< , it will save you and us time !
cheers
Your answer
Follow this Question
Related Questions
Reload manually in raycast? 1 Answer
Weapon reload timer problem. 2 Answers
Reload with keypress work bad! 0 Answers
Switch Weapon Script Not taking effect HELP!! 2 Answers
Best way to Animate with models that interact with other models? 0 Answers