Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Adding object to layer

Discussion in 'Getting Started' started by notjake85, Apr 2, 2024.

  1. notjake85

    notjake85

    Joined:
    Feb 20, 2024
    Posts:
    45
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Weapon : MonoBehaviour
    7. {
    8.    
    9.     public bool isActiveWeapon;
    10.  
    11.     [Header("Shooting")]
    12.     //Shooting
    13.     public bool isShooting, readyToShoot;
    14.     bool allowReset = true;
    15.     public float shootingDelay = 2f;
    16.  
    17.     [Header("Burst")]
    18.     //Burst
    19.     public int bulletsPerBurst = 3;
    20.     public int burstBulletsLeft;
    21.  
    22.     [Header("Spread")]
    23.     //Spread
    24.     public float spreadIntensity;
    25.     public float hipSpreadIntensity;
    26.     public float adsSpreadIntensity;
    27.  
    28.     [Header("Bullet")]
    29.     //Bullet
    30.     public GameObject bulletPrefab;
    31.     public Transform bulletSpawn;
    32.     public float bulletVelocity = 30;
    33.     public float bulletPrefabLifeTime = 3f;
    34.  
    35.     public GameObject muzzleEffect;
    36.     internal Animator animator;
    37.  
    38.     [Header("Loading")]
    39.     //Loading
    40.     public float reloadTime;
    41.     public int magazineSize, bulletsLeft;
    42.     public bool isReloading;
    43.  
    44.     bool isADS;
    45.  
    46.     [Header("In hand position and model/shooting mode")]
    47.     //In hand position
    48.     public Vector3 spawnPosition;
    49.     public Vector3 spawnRotation;
    50.  
    51.    
    52.     public enum WeaponModel
    53.     {
    54.         SVA_545,
    55.         FF_Magnum,
    56.         Glock
    57.     }
    58.  
    59.     public WeaponModel thisWeaponModel;
    60.  
    61.     public enum ShootingMode
    62.     {
    63.         Single,
    64.         Burst,
    65.         Auto
    66.     }
    67.  
    68.     public ShootingMode currentShootingMode;
    69.  
    70.     private void Awake()
    71.     {
    72.         readyToShoot = true;
    73.         burstBulletsLeft = bulletsPerBurst;
    74.         animator = GetComponent<Animator>();
    75.  
    76.         bulletsLeft = magazineSize;
    77.  
    78.         spreadIntensity = hipSpreadIntensity;
    79.  
    80.     }
    81.  
    82.  
    83.     void Update()
    84.     {
    85.  
    86.         if (isActiveWeapon)
    87.         {
    88.  
    89.             foreach (Transform child in transform)
    90.             {
    91.                 child.gameObject.layer = LayerMask.NameToLayer("WeaponRender");
    92.             }
    93.  
    94.  
    95.             if (Input.GetMouseButtonDown(1))
    96.             {
    97.                 enterADS();
    98.             }
    99.            
    100.             if (Input.GetMouseButtonUp(1))
    101.             {
    102.                 exitADS();
    103.             }
    104.  
    105.  
    106.             GetComponent<Outline>().enabled = false;
    107.            
    108.             if(bulletsLeft == 0 && isShooting)
    109.             {
    110.                 SoundManager.Instance.emptyMagazine.Play();
    111.             }
    112.  
    113.  
    114.             if (currentShootingMode == ShootingMode.Auto)
    115.             {
    116.                 //Holding Down Left Mouse Button
    117.                 isShooting = Input.GetKey(KeyCode.Mouse0);
    118.             }
    119.             else if (currentShootingMode == ShootingMode.Single ||
    120.                 currentShootingMode == ShootingMode.Burst)
    121.             {
    122.                 //Clicking Left Mouse Button Once
    123.                 isShooting = Input.GetKeyDown(KeyCode.Mouse0);
    124.             }
    125.  
    126.             if(Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && isReloading == false && WeaponManager.Instance.CheckAmmoLeftFor(thisWeaponModel) > 0)
    127.             {
    128.                 Reload();
    129.             }
    130.  
    131.             if (readyToShoot && isShooting && bulletsLeft > 0l)
    132.             {
    133.                 burstBulletsLeft = bulletsPerBurst;
    134.                 FireWeapon();
    135.             }
    136.             else
    137.             {
    138.                 foreach (Transform child in transform)
    139.                 {
    140.                     child.gameObject.layer = LayerMask.NameToLayer("Default");
    141.                 }
    142.             }
    143.         }
    144.     }
    145.  
    146.     private void enterADS()
    147.     {
    148.         animator.SetTrigger("enterADS");
    149.         isADS = true;
    150.         HUDmanager.Instance.middleDot.SetActive(false);
    151.         spreadIntensity = adsSpreadIntensity;
    152.     }
    153.  
    154.     private void exitADS()
    155.     {
    156.         animator.SetTrigger("exitADS");
    157.         isADS = false;
    158.         HUDmanager.Instance.middleDot.SetActive(true);
    159.         spreadIntensity = hipSpreadIntensity;
    160.     }
    161.  
    162.    
    163.  
    164.     private void FireWeapon()
    165.     {
    166.         bulletsLeft--;
    167.  
    168.         muzzleEffect.GetComponent<ParticleSystem>().Play();
    169.  
    170.         if (isADS)
    171.         {
    172.             animator.SetTrigger("Recoil_ADS");
    173.         }
    174.  
    175.         else
    176.         {
    177.             animator.SetTrigger("Recoil");
    178.         }
    179.  
    180.         SoundManager.Instance.PlayShootingSound(thisWeaponModel);
    181.  
    182.         readyToShoot = false;
    183.  
    184.         Vector3 shootingDirection = CalculateDirectionAndSpread().normalized;
    185.  
    186.         //Instantiate the bullet
    187.         GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, Quaternion.identity);
    188.        
    189.         //Pointing the bullet
    190.         bullet.transform.forward = shootingDirection;
    191.  
    192.         //Shoot the bullet
    193.         bullet.GetComponent<Rigidbody>().AddForce(shootingDirection * bulletVelocity, ForceMode.Impulse);
    194.        
    195.         //Destroy the bullet after some time
    196.         StartCoroutine(DestroyBulletAfterTime(bullet, bulletPrefabLifeTime));
    197.  
    198.         //Checking if we are done shooting
    199.         if (allowReset)
    200.         {
    201.             Invoke("ResetShot", shootingDelay);
    202.             allowReset = false;
    203.         }
    204.  
    205.         //Burst Mode
    206.         if (currentShootingMode == ShootingMode.Burst && burstBulletsLeft > 1)//we already shot once before this check
    207.         {
    208.             burstBulletsLeft--;
    209.             Invoke("FireWeapon", shootingDelay);
    210.         }
    211.     }
    212.  
    213.     private void Reload()
    214.     {
    215.         SoundManager.Instance.PlayReloadSound(thisWeaponModel);
    216.  
    217.         animator.SetTrigger("Reload");
    218.  
    219.         isReloading = true;
    220.         Invoke("reloadCompleted", reloadTime);
    221.     }
    222.  
    223.     private void reloadCompleted()
    224.     {
    225.         if (WeaponManager.Instance.CheckAmmoLeftFor(thisWeaponModel) > magazineSize)
    226.         {
    227.             bulletsLeft = magazineSize;
    228.             WeaponManager.Instance.DecreaseTotalAmmo(bulletsLeft, thisWeaponModel);
    229.         }
    230.         else
    231.         {
    232.             bulletsLeft = WeaponManager.Instance.CheckAmmoLeftFor(thisWeaponModel);
    233.             WeaponManager.Instance.DecreaseTotalAmmo(bulletsLeft, thisWeaponModel);
    234.         }
    235.  
    236.         isReloading = false;
    237.     }
    238.  
    239.  
    240.     private void ResetShot()
    241.     {
    242.         readyToShoot = true;
    243.         allowReset = true;
    244.     }
    245.  
    246.     public Vector3 CalculateDirectionAndSpread()
    247.     {
    248.         //Shooting fron the middle of the screen to check where we are pointing at
    249.         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    250.         RaycastHit hit;
    251.  
    252.         Vector3 targetPoint;
    253.         if (Physics.Raycast(ray, out hit))
    254.         {
    255.             //Hitting something
    256.             targetPoint = hit.point;
    257.         }
    258.         else
    259.         {
    260.             //Shooting at the air
    261.             targetPoint = ray.GetPoint(100);
    262.         }
    263.  
    264.         Vector3 direction = targetPoint - bulletSpawn.position;
    265.  
    266.         float z = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
    267.         float y = UnityEngine.Random.Range(-spreadIntensity, spreadIntensity);
    268.  
    269.         //Returning the shooting direction and spread
    270.         return direction + new Vector3(0, y, z);
    271.     }
    272.  
    273.     private IEnumerator DestroyBulletAfterTime(GameObject bullet, float delay)
    274.     {
    275.         yield return new WaitForSeconds(delay);
    276.         Destroy(bullet);
    277.     }
    278. }
    279.  
    In the update function i have a line of code to add my gameObject to a layer but it is not going there. It is activating the isActive bool but not changing the layer?

    Any clue why?