0
\$\begingroup\$

I am trying to get the players gun to fire when the player is walking and the stop firing when the player starts to sprint. The gun will work just fine and ire when the player is walking, but it will continue to fire when they start running and they can also start firing when they are running. I have been trying or most o the day and have not been able to figure out how to fix this.

The top script is obviously the script or the gun firing and whatnot and the bottom script is the controller or movement as well as the animation for the gun.

EDIT: Still no luck.

public class Shooting : MonoBehaviour
{
    private float NextTimeToFire = 0f;
    public bool AllowedToShoot = true;

[Space(10)]
    [Header("Floats")]
    public float Damage = 10.0f;
    public float Range = 100.0f;
    public float ImpactForce = 60f;
    public float FireRate = 15f;

    [Space(10)]
    [Header("Others")]
    [Space(5)]
    public Camera FPSCamera;
    public ParticleSystem MuzzleFlash;
    public GameObject impactEffect;

void Start()
    {
    }

    void Update()
    {       
            if (AllowedToShoot == false) return;
            {
            if (Input.GetButton("Fire1") && Time.time >= NextTimeToFire)
                {
                    NextTimeToFire = Time.time + 1f / FireRate;
                    Shoot();
                   }     
            }
    }

    void Shoot()
    {
        MuzzleFlash.Play();

            RaycastHit hit;
            if (Physics.Raycast(FPSCamera.transform.position, FPSCamera.transform.forward, out hit, Range))
        {
            Debug.Log(hit.transform.name);
            Objects enemy = hit.transform.GetComponent<Objects>();
            if (enemy != null)
            {
                enemy.TakeDamage(Damage);
            }

            if (hit.rigidbody != null)
            {
                    hit.rigidbody.AddForce(-hit.normal * ImpactForce);
            }
            GameObject ImpactGo = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
            Destroy(ImpactGo, 2f);
        }
    }
}

///

public class WeaponMovement : MonoBehaviour
{
    public GameObject CameraAnimations;
    Animator anim;

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

    void Update()
    {
        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
            {
                anim.SetBool("Sprint", true);
            }
        }

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

        if (Input.GetKeyUp(KeyCode.LeftShift) || Input.GetKeyUp(KeyCode.RightShift))
        {
            anim.SetBool("Sprint", false);
        }
    }   
}
\$\endgroup\$
  • \$\begingroup\$ If your using a rigidbody, you could add an if condition to the shoot method checking to see if the velocity is less than the set run velocity. \$\endgroup\$ – Millard Aug 20 '19 at 18:58
1
\$\begingroup\$

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
|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Do I add this to the Recoil or WeaponMovement script? \$\endgroup\$ – Ethan K-B Aug 14 '19 at 16:49
  • \$\begingroup\$ @EthanK-B every where you cal your spring animation if you dont to shoot on sprint \$\endgroup\$ – virtouso Aug 14 '19 at 17:45
  • \$\begingroup\$ @EthanK-B test your game. that is the only way to can make sure your code is right \$\endgroup\$ – virtouso Aug 14 '19 at 18:18
  • \$\begingroup\$ I it helps I changed my question a bit as well as the script to make it easier to work with. \$\endgroup\$ – Ethan K-B Aug 15 '19 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.