hey guys, im trying to create a script where when I press R the gun the character is holding will reload. So far ive managed to get as far as to when the character shoots 8 times the gun cannot shoot anymore. however I cannot get the key R to make the gun reload so the MaxAmmo goes back to 8 again, I really hope someone can help me with this because ive been stuck on it for a while now
my code:
using UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
public float fireRate = 0;
public float Damage = 10;
public LayerMask whatToHit;
public float reloadTime = 1; //i wanted a time gap as this would make it more realistic
public int maxAmmo = 8; //this is the maximum bullets i would like the pistol to have
public int zeroAmmo = 0; //was going to use this to determine whether the gun should be reloaded or not
public Transform BulletTrailPrefab;
public Transform MuzzleFlashPrefab;
float timeToSpawnEffect = 0;
public float effectSpawnRate = 10;
float timeToFire = 0;
Transform firePoint;
// Use this for initialization
void Awake () {
firePoint = transform.FindChild ("FirePoint");
if (firePoint == null) {
Debug.LogError ("No firePoint? WHAT?!");
}
}
// Update is called once per frame
void Update () {
if (fireRate == 0) {
if (Input.GetButtonDown ("Fire1")) {
Shoot();
Reload();
}
}
else {
if (Input.GetButton ("Fire1") && Time.time > timeToFire) {
timeToFire = Time.time + 1/fireRate;
Shoot();
}
}
}
void Reload ()
{
if (maxAmmo <= zeroAmmo && Input.GetKey(KeyCode.R)) //this is where i got stuck on what to do, i was trying to make R the key to reload/Set maxAmmo back to 8 so the player can shoot again
{
maxAmmo = 8; //when the player presses R the maxAmmo resets back to 8 to allow him to shoot again... this is the part that is not working
}
}
void Shoot () {
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
if (Time.time >= timeToSpawnEffect) {
timeToSpawnEffect = Time.time + 1/effectSpawnRate;
if (Input.GetButtonDown ("Fire1") && maxAmmo > zeroAmmo) { //player can only shoot when the maxAmmo has a greater value than zeroValue
Effect ();
audio.Play ();
maxAmmo = maxAmmo -1; //So when the player shoots it deducts 1 from the maxAmmo count
Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
if (hit.collider != null) {
Debug.DrawLine (firePointPosition, hit.point, Color.red);
Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage.");
}
}
}
}
void Effect () {
Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
clone.parent = firePoint;
float size = Random.Range (0.6f, 0.9f);
clone.localScale = new Vector3 (size, size, size);
Destroy (clone.gameObject, 0.02f);
}
}