Issues with Fire Rate
So I'm new to Unity and been following a lot of Brackey's tutorials to find my way. I want to make some sort of FPS game so I watched "Shooting with Raycasts - Unity Tutorial"
It all works until I add the fire rate in and then it just seems to not work. When I use the code below, the gun shoots full auto since it is "GetButton" but the fire rate doesn't seem to come into play at all. Can anyone help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunScript : MonoBehaviour
{
public float damage = 25f;
public float range = 100f;
public AudioSource gunShot;
public float fireRate = 1f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
private float nextTimeToFire = 0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
void Shoot()
{
muzzleFlash.Play();
gunShot.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Enemy target = hit.transform.GetComponent<Enemy>();
if(target != null)
{
target.TakeDamage(damage);
}
}
}
}
Your answer
Follow this Question
Related Questions
Reloading with R only goes up by 1. But reloading when the gun is empty works fine. 0 Answers
How to call Animator from another script 0 Answers
i'm getting error CS1014 when putting a C# scripting into something. 1 Answer
Character swap breaks movement. 1 Answer
Register.cs(31,17): error CS0029: Cannot implicitly convert type `string' to `bool' 1 Answer