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);
}
}
}