Search Unity

Better Way to Organise Weapon Types

Discussion in 'Scripting' started by Vector5_, Aug 24, 2021.

  1. Vector5_

    Vector5_

    Joined:
    Jun 20, 2021
    Posts:
    1
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OBJECTS_WEAPONS : MonoBehaviour
    6. {
    7.     [SerializeField] string weaponType;
    8.     [SerializeField] Transform origin;
    9.     [SerializeField] LineRenderer trail;
    10.     [SerializeField] GameObject bullet;
    11.  
    12.     float range, cooldownSeconds, damage, inaccuracy, bulletSpeed;
    13.  
    14.     bool shooting;
    15.     int cooldownFrames;
    16.     float shotInaccuracyX, shotInaccuracyY;
    17.  
    18.     Vector3 shotDir;
    19.     GameObject currentBullet;
    20.  
    21.     IEnumerator Shoot()
    22.     {
    23.         if(weaponType == "gun.pistolBasic")
    24.         {
    25.             range = 15;
    26.             cooldownSeconds = 0.25f;
    27.             damage = 1;
    28.             inaccuracy = 0.02f;
    29.             bulletSpeed = 500;
    30.  
    31.             cooldownFrames = Mathf.FloorToInt(cooldownSeconds * 50);
    32.  
    33.             shotInaccuracyX = Random.Range(-inaccuracy, inaccuracy);
    34.             shotInaccuracyY = Random.Range(-inaccuracy, inaccuracy);
    35.  
    36.             shotDir = origin.forward + new Vector3(shotInaccuracyY, shotInaccuracyX, 0);
    37.  
    38.             currentBullet = Instantiate(bullet) as GameObject;
    39.  
    40.             currentBullet.transform.position = origin.transform.position;
    41.             currentBullet.transform.rotation = Quaternion.Euler(0, 0, origin.transform.rotation.x);
    42.             currentBullet.GetComponent<Rigidbody2D>().velocity = shotDir * bulletSpeed;
    43.             currentBullet.GetComponent<OBJECTS_BULLETS_BULLETBASIC>().bulletDamage = damage;
    44.  
    45.             GetComponent<AudioSource>().Play();
    46.         }
    47.         yield return null;
    48.     }
    49.  
    50.     private void Update()
    51.     {
    52.         if (Input.GetMouseButton(0))
    53.         {
    54.             shooting = true;
    55.         }
    56.         else
    57.         {
    58.             shooting = false;
    59.         }
    60.  
    61.         if(cooldownFrames == 0 & shooting)
    62.         {
    63.             StartCoroutine(Shoot());
    64.         }
    65.     }
    66.  
    67.     private void FixedUpdate()
    68.     {
    69.         if (cooldownFrames > 0)
    70.         {
    71.             cooldownFrames--;
    72.         }
    73.     }
    74. }
    75.  
    Any help is appreciated!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201