I have no idea what to do. I got this script from Brackeys and followed it step by step but it gives me this error.
using UnityEngine;
using System.Collections;
public class AK47 : MonoBehaviour
{
public float damage = 30f;
public float range = 200f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public float impactForce = 30f;
public float fireRate = 15f;
public int maxAmmo = 30;
private int currentAmmo;
public float reloadTime = 3f;
private float nextTimeToFire = 0f;
private bool isReloading = false;
void Start()
{
currentAmmo = maxAmmo;
}
void Update()
{
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
muzzleFlash.Play();
currentAmmo--;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if(hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
IEnumerator Reload ()
{
Debug.Log("Reloading...");
isReloading = true;
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
}
}
}
Two problems :
-
You added the IEnumerator Reload () inside the Shoot()
Take it out and put it under the Shoot.
-
You have to add at the top
using System.Collections.Generic;
So your script in the end should look like this :
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class AK47 : MonoBehaviour
{
public float damage = 30f;
public float range = 200f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
public float impactForce = 30f;
public float fireRate = 15f;
public int maxAmmo = 30;
private int currentAmmo;
public float reloadTime = 3f;
private float nextTimeToFire = 0f;
private bool isReloading = false;
void Start()
{
currentAmmo = maxAmmo;
}
void Update()
{
if (isReloading)
return;
if (currentAmmo <= 0)
{
StartCoroutine(Reload());
return;
}
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
muzzleFlash.Play();
currentAmmo--;
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
IEnumerator Reload()
{
Debug.Log("Reloading...");
isReloading = true;
yield return new WaitForSeconds(reloadTime);
currentAmmo = maxAmmo;
isReloading = false;
}
}
Thank you so much! I think I’m getting blind cause I triple checked the IEnumerator.