- Home /
Other
Gun reload and clip size
First Problem: I can only shoot 6 times instead of 10.
Second Problem: When the clip is empty I can't shoot anymore because it doesn't reload. I think that the yield is the problem.
Third Problem: 'GetButtonDown(KeyCode.R)' doesn't work although monodevelop recommend it
..............................................................................................................................................................
var projectile : Rigidbody;
var speed = 5;
var fireRate : float = 0.1;
var clone : Rigidbody;
var weapon : GameObject;
var clipsLeft : int = 5;
var clipSize : int = 10;
private var ammo : int;
private var nextFire = 0.0;
private var wait : boolean;
private var reloadTime : float = 1.5;
function Start ()
{
Screen.showCursor = false;
ammo = clipSize;
}
function Update () {
if(Input.GetButton("Fire1") && Time.time > nextFire && wait == false)
{
if(ammo > 0){
nextFire = Time.time + fireRate;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
Destroy (clone.gameObject, 5);
audio.Play();
ammo--;
if(ammo <= 0)
{
Reload ();
}
}
}
if(Input.GetButtonDown("R"))
{
Reload ();
}
}
function Reload () {
if(clipsLeft > 0) {
if (ammo >0){
clipsLeft--;
wait = true;
audio.play();
yield WaitForSeconds(reloadTime);
ammo = clipSize + 1;
wait = false;
}
else {
clipsLeft--;
wait = true;
audio.play();
yield WaitForSeconds(reloadTime);
ammo = clipSize;
wait = false;
}
}
}
For your third problem, use Input.Get$$anonymous$$eyDown():
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) {
Given the double spacing and lack of indentation, your code was difficult to read, and I could not see any problems that would result in the errors you are having. So I cleaned the code up a bit. The only issue I found was the low setting 'fireRate'. Being small, I was getting two overlapping/colliding projectiles. When I change the value to 0.2, the two other issues you mention work...fired a full 10 rounds, paused and reloaded when empty. Here is the code I tested:
#pragma strict
var projectile : Rigidbody;
var speed : float = 5.0;
var fireRate : float = 0.1;
var weapon : GameObject;
var clipsLeft : int = 5;
var clipSize : int = 10;
private var clone : Rigidbody;
private var ammo : int;
private var nextFire = 0.0;
private var wait : boolean;
private var reloadTime : float = 1.5;
function Start ()
{
Screen.showCursor = false;
ammo = clipSize;
}
function Update ()
{
if (Input.GetButton("Fire1") && (Time.time > nextFire) && wait == false)
{
if(ammo > 0)
{
nextFire = Time.time + fireRate;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.forward * speed;
Destroy (clone.gameObject, 5);
//audio.Play();
ammo--;
if(ammo <= 0)
{
Reload ();
}
}
}
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R))
{
Reload ();
}
}
function Reload ()
{
Debug.Log("Reload");
if(clipsLeft > 0)
{
if (ammo >0)
{
clipsLeft--;
wait = true;
audio.Play();
yield WaitForSeconds(reloadTime);
ammo = clipSize + 1;
wait = false;
}
else
{
clipsLeft--;
wait = true;
audio.Play();
yield WaitForSeconds(reloadTime);
ammo = clipSize;
wait = false;
}
}
}
thank you for your quick answer the problem with keycode.r was that I wrote 'getBUTTON' ins$$anonymous$$d of 'get$$anonymous$$EY' the clipsize was 6 in the object inspector don't know why. the reload problem was caused by audio.Play() don't know why
Follow this Question
Related Questions
Reload Ammo is not working 1 Answer
adding a delay to shooting 2 Answers
Reload help 1 Answer
Reload Ammo is not working 0 Answers