How do I make the gun stop shooting when sprinting

by Ethan K-B   Last Updated August 14, 2019 07:13 AM

I want the player gun to stop shooting when the player is sprinting. Right now if the player is shooting and then starts to sprint the gun will keep playing the shooting animation.

    public class WeaponMovement : MonoBehaviour
    {

        public GameObject CameraAnimations;
        public bool StopShooting = true;
        public Recoil RecoilScript;
        Animator anim;

        private void Awake()
        {
            anim = CameraAnimations.GetComponent<Animator>();
        }

        void Update()
        {
            if (StopShooting == false) return;
            {
                if (Input.GetKey(KeyCode.W))
                {
                    if (Input.GetKey(KeyCode.LeftShift))
                    {             
                        RecoilScript.RecoilAnimation = true;
                    }
                }
            }




        if (Input.GetKey(KeyCode.W))
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                anim.SetBool("Sprint", true);
            }
        }

        //This section is key up.

        if (Input.GetKeyUp(KeyCode.W))
        {
            anim.SetBool("Sprint", false);
        }

        if (Input.GetKeyUp(KeyCode.LeftShift))
        {
            anim.SetBool("Sprint", false);
        }
    }
}

.

public class Recoil : MonoBehaviour
{
    public bool RecoilAnimation = true;
    public WeaponMovement stop;
    GameObject recoil;
    Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonUp("Fire1"))
        {
            anim.SetBool("IsShooting", false);
        }
        if (Input.GetButtonDown("Fire1") && GetComponentInParent<PlayerController>().moving == false)
        {
            anim.SetBool("IsShooting", true);
            anim.SetBool("UpToDown", true);
            anim.SetBool("DownToIdle", true);
        }

        if (RecoilAnimation == false) return;
        {
            stop.StopShooting = true;
            if (Input.GetButtonDown("Fire1") && GetComponentInParent<PlayerController>().moving == false)
            {
                anim.SetBool("IsShooting", true);
                anim.SetBool("UpToDown", true);
                anim.SetBool("DownToIdle", true);
            }

            if (Input.GetButtonUp("Fire1"))
            {
                anim.SetBool("IsShooting", false);
            }
        }
    }
}
Tags : unity c#


Answers 1


you can use GetBool("State Name") function to check sprint any where. but right Animator design is always the key to freedom on changing you character behaviour. this is my example code:

bool sprint= anim.getBool("sprint");
if(sprint) return;
//you shooting logic here
virtouso
virtouso
August 14, 2019 06:27 AM

Related Questions


Unfortunately game has stopped

Updated July 02, 2019 11:13 AM

How to animate objects in Unity 3D

Updated July 12, 2015 13:05 PM