Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Animation that includes camera keyframes make the camera unusable in game

Discussion in 'Animation' started by spacegamer179, Feb 7, 2022.

  1. spacegamer179

    spacegamer179

    Joined:
    May 10, 2021
    Posts:
    1
    It s a gun reload animation and it s stopping all the other animations and make the fps camera unusable.
    I am using 2 animators, one for idle,shoot and one for reload that I put in the camera, and the camera is the parent of the gun.

    If I use an override then the reload animation dosen t work.

    This is the script for the gun.

    If I need to give more information I will give more.

    Code (CSharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class shoot : MonoBehaviour
    5. {
    6.     //Gun stats
    7.     public int damage;
    8.     public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
    9.     public int magazineSize, bulletsPerTap;
    10.     public bool allowButtonHold;
    11.     int bulletsLeft, bulletsShot;
    12.  
    13.     //bools
    14.     bool shooting, readyToShoot, reloading;
    15.  
    16.     //Reference
    17.     public Camera fpsCam;
    18.     public Transform attackPoint;
    19.     public RaycastHit rayHit;
    20.     public LayerMask whatIsEnemy;
    21.  
    22.  
    23.     //Graphics
    24.     public GameObject muzzleFlash, bulletHoleGraphic;
    25.     public CameraShake camShake;
    26.     public float camShakeMagnitude, camShakeDuration;
    27.     public TextMeshProUGUI text;
    28.  
    29.    
    30.  
    31.  
    32.     public Animator anim;
    33.     public Animator anim2;
    34.  
    35.  
    36.     public AudioClip Shoott;
    37.     public AudioClip reload;
    38.  
    39.     public AudioSource aud;
    40.  
    41.     public GameObject gun;
    42.  
    43.     private void Awake()
    44.     {
    45.         bulletsLeft = magazineSize;
    46.         readyToShoot = true;
    47.        
    48.     }
    49.     private void LateUpdate()
    50.     {
    51.         MyInput();
    52.  
    53.         //SetText
    54.         text.SetText(bulletsLeft + " / " + magazineSize);
    55.  
    56.      
    57.     }
    58.     private void MyInput()
    59.     {
    60.         if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
    61.         else shooting = Input.GetKeyDown(KeyCode.Mouse0);
    62.  
    63.         if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
    64.  
    65.         //Shoot
    66.         if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
    67.         {
    68.             bulletsShot = bulletsPerTap;
    69.             Shoot();
    70.             anim.SetBool("shoot", true);
    71.             Invoke("Test", 0.7f);
    72.             aud.PlayOneShot(Shoott);
    73.         }
    74.        
    75.     }
    76.     private void Shoot()
    77.     {
    78.         readyToShoot = false;
    79.  
    80.         //Spread
    81.         float x = Random.Range(-spread, spread);
    82.         float y = Random.Range(-spread, spread);
    83.  
    84.         //Calculate Direction with Spread
    85.         Vector3 direction = gun.transform.right * -1 + new Vector3(x, y, 0);
    86.  
    87.         //RayCast
    88.         if (Physics.Raycast(gun.transform.position, direction, out rayHit, range, whatIsEnemy))
    89.         {
    90.             Debug.Log(rayHit.collider.name);
    91.            
    92.  
    93.         }
    94.  
    95.         //ShakeCamera
    96.         StartCoroutine( camShake.Shake(camShakeDuration, camShakeMagnitude));
    97.  
    98.         //Graphics
    99.         Instantiate(bulletHoleGraphic, rayHit.point + new Vector3(0, 0, -0.5f), Quaternion.Euler(0, 0, 0));
    100.         Instantiate(muzzleFlash, attackPoint.position, attackPoint.rotation);
    101.  
    102.        
    103.  
    104.         bulletsLeft--;
    105.         bulletsShot--;
    106.  
    107.         Invoke("ResetShot", timeBetweenShooting);
    108.  
    109.         if (bulletsShot > 0 && bulletsLeft > 0)
    110.             Invoke("Shoot", timeBetweenShots);
    111.     }
    112.     private void ResetShot()
    113.     {
    114.         readyToShoot = true;
    115.     }
    116.     private void Reload()
    117.     {
    118.        
    119.         anim2.SetBool("Reload", true);
    120.         aud.PlayOneShot(reload);
    121.         reloading = true;
    122.         Invoke("ReloadFinished", reloadTime);
    123.         Invoke("Test2", 6);
    124.     }
    125.     private void ReloadFinished()
    126.     {
    127.         bulletsLeft = magazineSize;
    128.         reloading = false;
    129.     }
    130.  
    131.     void Test()
    132.     {
    133.         anim.SetBool("shoot", false);
    134.     }
    135.     void Test2()
    136.     {
    137.         anim2.SetBool("Reload", false);
    138.      
    139.     }
    140. }
     

    Attached Files: