ill leave the code below, but shouldn’t the new weapon be exactly the same prefab?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour {
public static Weapon instance;
public float fireRate = 0;
public int damage = 10;
public float fireDistance = 100;
public LayerMask whatToHit;
public int maxAmmo = 10;
public int curAmmo;
public float reloadTime = 1f;
private bool isReloading = false;
public Transform BulletTrailPrefab;
public Transform HitPrefab;
public Transform MuzzleFlashPrefab;
float timeToSpawnEffect = 0;
public float effectSpawnRate = 10;
//Handle camera shaking
public float camShakeAmount = 0.05f;
public float camShakeLength = 0.1f;
CameraShake camShake;
public string weaponShootSound = “DefaultShot”;
float timeToFire = 0;
float muzzleMinSize = 0.6f;
float muzzleMaxSize = 0.9f;
float muzzleTimer = 0.02f;
float trailTimer = 0.04f;
float particleLifetime = 1f;
Transform firePoint;
//Caching
AudioManager audioManager;
// Use this for initialization
void Awake () {
firePoint = transform.Find(“FirePoint”);
if (firePoint == null) {
Debug.LogError(“No FirePoint”);
}
if (instance == null) {
instance = this;
}
}
void Start() {
curAmmo = maxAmmo;
camShake = GameMaster.gm.GetComponent<CameraShake>();
if (camShake == null) {
Debug.LogError(“No CameraShake script found on GM object”);
}
audioManager = AudioManager.instance;
if (audioManager == null) {
Debug.LogError(“No audioManager found”);
}
}
void OnEnable() {
isReloading = false;
}
// Update is called once per frame
void Update () {
if (isReloading == true) {
return;
}
//Press R to reload (make sure to write stuff on top of this, return stops the function)
if (Input.GetKeyDown(KeyCode.R)) {
StartCoroutine(Reload());
return;
}
if (curAmmo <= 0) {
StartCoroutine(Reload());
return;
}
if (fireRate == 0) {
if (Input.GetButtonDown (“Fire1”)) {
Shoot();
}
}
else {
if (Input.GetButton (“Fire1”) && Time.time > timeToFire) {
timeToFire = Time.time + 1 / fireRate;
Shoot();
}
}
}
IEnumerator Reload() {
isReloading = true;
Debug.Log(“Reloading…”);
yield return new WaitForSeconds(reloadTime);
curAmmo = maxAmmo;
isReloading = false;
}
void Shoot() {
curAmmo–;
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, fireDistance, whatToHit);
Debug.DrawLine (firePointPosition, (mousePosition – firePointPosition) * 100, Color.cyan);
if (hit.collider != null) {
Debug.DrawLine(firePointPosition, hit.point, Color.red);
Enemy enemy = hit.collider.GetComponent<Enemy>();
if (enemy != null) {
enemy.DamageEnemy(damage);
//Debug.Log(“We hit ” + hit.collider.name + ” and did ” + damage + ” damage “);
}
}
if (Time.time >= timeToSpawnEffect) {
Vector3 hitPos;
Vector3 hitNormal;
if (hit.collider == null) {
hitPos = (mousePosition – firePointPosition) * 30;
hitNormal = new Vector3(9999, 9999, 9999);
}
else {
hitPos = hit.point;
hitNormal = hit.normal;
}
Effect(hitPos, hitNormal);
timeToSpawnEffect = Time.time + 1 / effectSpawnRate;
}
}
void Effect(Vector3 hitPos, Vector3 hitNormal) {
Transform trail = Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation) as Transform;
LineRenderer lr = trail.GetComponent<LineRenderer>();
if (lr != null) {
lr.SetPosition(0, firePoint.position); //0 is for element 0
lr.SetPosition(1, hitPos); //1 is for element 1
}
Destroy(trail.gameObject, trailTimer);
if (hitNormal != new Vector3(9999, 9999, 9999)) {
Transform hitParticle = Instantiate(HitPrefab, hitPos, Quaternion.FromToRotation(Vector3.forward, hitNormal)) as Transform;
Destroy(hitParticle.gameObject, particleLifetime);
}
Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
clone.parent = firePoint;
float size = Random.Range (muzzleMinSize, muzzleMaxSize);
clone.localScale = new Vector3 (size, size, size);
Destroy (clone.gameObject, muzzleTimer);
//Shake the camera
camShake.Shake(camShakeAmount, camShakeLength);
//Play shoot sound
audioManager.PlaySound(weaponShootSound);
}
}