Sunday, October 2, 2022
HomeGame Developmentunity - Object delets on reload (Animation)

unity – Object delets on reload (Animation)


So i’ve a script that performs an animation of the gun Aiming Down Sights, and when the Reload() perform is named, if that animation is energetic, the thing deletes (which isn’t what i need it to do)

So heres my scripts:

Aiming Down Sights:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class ADS : MonoBehaviour
{

    public SportObject Gun; 
    // Start is named earlier than the primary body replace
    void Start()
    {
        
        
    }

    // Update is named as soon as per body
    void Update()
    {
        if(Input.GetMouseButtonDown(1)) {
            Gun.GetComponent<Animator>().Play("ADS");
        }

        if(Input.GetMouseButtonUp(1)) {
            Gun.GetComponent<Animator>().Play("New State");
        }
    }
}

Gun:


utilizing UnityEngine;
utilizing System.Collections; 

public class Gun : MonoBehaviour
{

    public float injury = 10f; 

    public float vary = 100f;

    public Camera fpsCam;

    public ParticleSystem muzzleFlash;

    public float fireRate = 4f; 

    personal recoil Recoil_Script;

    bool allowfire;

    public float subsequentTimeToFire= 0f; 

    AudioSource audioData; 

    public int maxAmmo = 10; 
    personal int currentAmmo; 
    public float reloadTime = 30f;

    personal bool isReloading = false;

    public void Start() {
        Recoil_Script = GetComponent<recoil>();

        audioData= GetComponent<AudioSource>();
        
        
        currentAmmo = maxAmmo;  

    }
    // Update is named as soon as per body
    void Update()
    {

        if (isReloading) {
            return; 
        }
        if (currentAmmo <= 0)
        {
            StartCoroutine(Reload());
            return;
        }
        if (Input.GetButton("Fire1") && Time.time >= subsequentTimeToFire) 
        {
            subsequentTimeToFire = Time.time + 1f/fireRate;
            Shoot();
        }

        if (Input.GetButtonDown("R")) {
            StartCoroutine(Reload());
        }
        

    }

    IEnumerator Reload() {
        isReloading = true; 
        Debug.Log("Reloading...");

        yield return new WaitForSeconds(reloadTime);

        currentAmmo = maxAmmo; 
        isReloading = false; 
    }

    void Shoot() {
        audioData.Play(); 
        muzzleFlash.Play();

        currentAmmo --; 


        

        
        Recoil_Script.RecoilFire();


        RaycastHit hit;
        if (Physics.Raycast(fpsCam.rework.place, fpsCam.rework.ahead, out hit, vary))
        {
            
            Debug.Log(hit.rework.identify);

            Target goal = hit.rework.GetComponent<Target>();
            if (goal  != null)
            {
                goal.TakeDamage(injury);

            }

           
            
            

            
        }


    }
}

Weapon Switching:

utilizing UnityEngine;

public class WeaponSwap : MonoBehaviour
{

    public int chosenWeapon = 0;


    // Start is named earlier than the primary body replace
    void Start()
    {
            ChooseWeapon();
    }

    // Update is named as soon as per body
    void Update()
    {
         int previousSelectedWeapon = chosenWeapon;
        if (Input.GetAxis("Mouse ScrollWheel") > 0f)
        {

           

            if (chosenWeapon >= rework.childCount - 1)
                chosenWeapon = 0; 
            else 
                chosenWeapon++;

             
        }
        if (Input.GetAxis("Mouse ScrollWheel") < 0f)
        {
            if (chosenWeapon <= 0)
                chosenWeapon = rework.childCount - 1;
            else 
                chosenWeapon--;  

        }

        if (Input.GetKeyDown(KeyCode.Alpha1) && rework.childCount >= 2)
        {
            chosenWeapon = 0; 
        }
        
        if (Input.GetKeyDown(KeyCode.Alpha2) && rework.childCount >= 2)
        {
            chosenWeapon = 1; 
        }
          if (Input.GetKeyDown(KeyCode.Alpha3) && rework.childCount >= 3)
        {
            chosenWeapon = 2; 
        }


        if (previousSelectedWeapon != chosenWeapon)
        {
            ChooseWeapon();
        }
        
    }
    void ChooseWeapon () {

        int i = 0;
        foreach (Transform weapon in rework) { 
            if (i == chosenWeapon) 
                weapon.recreationObject.SetActive(true);
            else 
                weapon.recreationObject.SetActive(false);
            i++; 

            
        } 
    }
}

If you want extra (screenshots, and so on) simply remark.

Thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments