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 known as, if that animation is energetic, the article deletes (which isn’t what i would like it to do)

So heres my scripts:

Aiming Down Sights:

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

public class ADS : MonoBehaviour
{

    public GameObject Gun; 
    // Begin is known as earlier than the primary body replace
    void Begin()
    {
        
        
    }

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

        if(Enter.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 Digital camera fpsCam;

    public ParticleSystem muzzleFlash;

    public float fireRate = 4f; 

    personal recoil Recoil_Script;

    bool allowfire;

    public float nextTimeToFire= 0f; 

    AudioSource audioData; 

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

    personal bool isReloading = false;

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

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

    }
    // Replace is known as as soon as per body
    void Replace()
    {

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

        if (Enter.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);

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

            }

           
            
            

            
        }


    }
}

Weapon Switching:

utilizing UnityEngine;

public class WeaponSwitch : MonoBehaviour
{

    public int selectedWeapon = 0;


    // Begin is known as earlier than the primary body replace
    void Begin()
    {
            SelectWeapon();
    }

    // Replace is known as as soon as per body
    void Replace()
    {
         int previousSelectedWeapon = selectedWeapon;
        if (Enter.GetAxis("Mouse ScrollWheel") > 0f)
        {

           

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

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

        }

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


        if (previousSelectedWeapon != selectedWeapon)
        {
            SelectWeapon();
        }
        
    }
    void SelectWeapon () {

        int i = 0;
        foreach (Remodel weapon in rework) { 
            if (i == selectedWeapon) 
                weapon.gameObject.SetActive(true);
            else 
                weapon.gameObject.SetActive(false);
            i++; 

            
        } 
    }
}

For those who want extra (screenshots, and so forth) simply remark.

Thanks

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments