I am very new to Unity and working on a 2D shooter. I would like advice on how to better organise the types of weapons that will be used in the game. I have researched on the concept of classes but I am unsure how to use them in this scenario.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OBJECTS_WEAPONS : MonoBehaviour
{
[SerializeField] string weaponType;
[SerializeField] Transform origin;
[SerializeField] LineRenderer trail;
[SerializeField] GameObject bullet;
float range, cooldownSeconds, damage, inaccuracy, bulletSpeed;
bool shooting;
int cooldownFrames;
float shotInaccuracyX, shotInaccuracyY;
Vector3 shotDir;
GameObject currentBullet;
IEnumerator Shoot()
{
if(weaponType == "gun.pistolBasic")
{
range = 15;
cooldownSeconds = 0.25f;
damage = 1;
inaccuracy = 0.02f;
bulletSpeed = 500;
cooldownFrames = Mathf.FloorToInt(cooldownSeconds * 50);
shotInaccuracyX = Random.Range(-inaccuracy, inaccuracy);
shotInaccuracyY = Random.Range(-inaccuracy, inaccuracy);
shotDir = origin.forward + new Vector3(shotInaccuracyY, shotInaccuracyX, 0);
currentBullet = Instantiate(bullet) as GameObject;
currentBullet.transform.position = origin.transform.position;
currentBullet.transform.rotation = Quaternion.Euler(0, 0, origin.transform.rotation.x);
currentBullet.GetComponent<Rigidbody2D>().velocity = shotDir * bulletSpeed;
currentBullet.GetComponent<OBJECTS_BULLETS_BULLETBASIC>().bulletDamage = damage;
GetComponent<AudioSource>().Play();
}
yield return null;
}
private void Update()
{
if (Input.GetMouseButton(0))
{
shooting = true;
}
else
{
shooting = false;
}
if(cooldownFrames == 0 & shooting)
{
StartCoroutine(Shoot());
}
}
private void FixedUpdate()
{
if (cooldownFrames > 0)
{
cooldownFrames--;
}
}
}
Any help is appreciated!