Search Unity

Cannot clone while mid-animation

Discussion in 'Editor & General Support' started by TyGy08, Dec 22, 2021.

  1. TyGy08

    TyGy08

    Joined:
    Sep 29, 2021
    Posts:
    3
    I am making a fps with one of the guns being a projectile-type rifle. With the animation, I am using booleans to switch between animations. When I run the animations using the code, the shooting animations block the cloneing, (at least I think that is what's happening) any ideas?





    Code (CSharp):
    1. //bullet
    2.     public GameObject bullet;
    3.  
    4.  
    5.     //bullet force
    6.     public float shootForce, upwardForce;
    7.  
    8.  
    9.     //gunstats
    10.     public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
    11.     public int magazineSize, bulletPerTap;
    12.     public bool allowButtonHold;
    13.    
    14.    
    15.     int bulletsLeft, bulletsShot;
    16.  
    17.     //WHAO GUN AMEK GO FLY AHHHHHH
    18.     public Rigidbody player;
    19.     public float recoilForce;
    20.  
    21.     //bools
    22.     public bool allowInvoke = true;
    23.  
    24.     //graphics
    25.     public GameObject muzzleFlash;
    26.     public TextMeshProUGUI wowAmmo;
    27.     bool shooting, readyToShoot, reloading;
    28.  
    29.     //reference
    30.     public Camera fpsCam;
    31.     public Transform attackPoint;
    32.     public Animator gunAnims;
    33.  
    34.  
    35.    
    36.  
    37.     private void Start()
    38.     {
    39.         //make sure magazine is full
    40.         bulletsLeft = magazineSize;
    41.         readyToShoot = true;
    42.         gunAnims = GetComponent<Animator>();
    43.  
    44.  
    45.     }
    46.  
    47.     private void Update()
    48.     {
    49.         MyInput();
    50.  
    51.         if (wowAmmo != null)
    52.             wowAmmo.SetText(bulletsLeft / bulletPerTap + "/" + magazineSize / bulletPerTap);
    53.  
    54.         if (Input.GetKeyUp(KeyCode.Mouse0))
    55.         {
    56.             gunAnims.SetBool("isShooting", false);
    57.         }
    58.  
    59.     }
    60.  
    61.  
    62.  
    63.  
    64.  
    65.  
    66.     private void MyInput()
    67.     {
    68.         //check is hold equal yasssss
    69.         if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
    70.         else shooting = Input.GetKeyDown(KeyCode.Mouse0);
    71.  
    72.  
    73.         //reloading
    74.         if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
    75.         //reload when bullet uh oh 0-0
    76.         if (readyToShoot && shooting && !reloading && bulletsLeft <= 0) Reload();
    77.  
    78.  
    79.  
    80.        
    81.        
    82.         //checking
    83.         if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
    84.         {
    85.             //setbullets shot to 0
    86.             bulletsShot = 0;
    87.  
    88.             Shooting();
    89.             gunAnims.SetBool("isShooting", true);
    90.         }
    91.     }
    92.  
    93.  
    94.     private void Shooting()
    95.     {
    96.      
    97.         readyToShoot = false;
    98.  
    99.         //idk man how far is stuff
    100.         Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
    101.         RaycastHit hit;
    102.         //does ray bonk
    103.         Vector3 targetPoint;
    104.         if (Physics.Raycast(ray, out hit))
    105.             targetPoint = hit.point;
    106.         else
    107.             targetPoint = ray.GetPoint(75);
    108.  
    109.         //sifshushdfisifuh math brain how far AHHHHHHHhhhhh
    110.         Vector3 directionWithOutspread = targetPoint - attackPoint.position;
    111.  
    112.  
    113.         float x = Random.Range(-spread, spread);
    114.         float y = Random.Range(-spread, spread);
    115.  
    116.  
    117.        
    118.  
    119.  
    120.  
    121.         //gigabrain direction
    122.         Vector3 directionWithSpread = directionWithOutspread + new Vector3(x, y, 0);
    123.  
    124.         //whoa bullet appear :0
    125.         GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity);
    126.         //rotata WHOOOOOOOOOOOOOOOOA
    127.         currentBullet.transform.forward = directionWithSpread.normalized;
    128.  
    129.  
    130.         //muzzleflash exists (gasp)
    131.         if (muzzleFlash != null)
    132.         {
    133.             Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
    134.             Invoke("DestroyMuzzle", 3);
    135.         }
    136.            
    137.        
    138.        
    139.        
    140.         //push bullet
    141.         currentBullet.GetComponent<Rigidbody>().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
    142.         currentBullet.GetComponent<Rigidbody>().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);
    143.  
    144.        
    145.  
    146.  
    147.  
    148.         bulletsLeft--;
    149.         bulletsShot++;
    150.  
    151.         //poke the script XD
    152.         if (allowInvoke)
    153.         {
    154.             Invoke("ResetShot", timeBetweenShooting);
    155.             allowInvoke = false;
    156.             //add recoil
    157.             player.AddForce(-directionWithSpread.normalized * recoilForce, ForceMode.Impulse);
    158.  
    159.         }
    160.  
    161.  
    162.         if (bulletsShot < bulletPerTap && bulletsLeft > 0)
    163.             Invoke("Shoot", timeBetweenShots);
    164.  
    165.    
    166.     }
    167.  
    168.     private void ResetShot()
    169.     {
    170.         readyToShoot = true;
    171.         allowInvoke = true;
    172.  
    173.        
    174.  
    175.     }
    176.  
    177.  
    178.     private void Reload()
    179.     {
    180.        
    181.         reloading = true;
    182.         gunAnims.SetBool("isReloading", true);
    183.         Invoke("ReloadFinished", reloadTime);
    184.        
    185.     }
    186.  
    187.     private void ReloadFinished()
    188.     {
    189.         bulletsLeft = magazineSize;
    190.         reloading = false;
    191.         gunAnims.SetBool("isReloading", false);
    192.     }
    193.  
    194.  
    195.    
    196.     private void DestroyMuzzle()
    197.     {
    198.         Destroy(muzzleFlash);
    199.     }
    200.