- Home /
Question by
lightyearz2 · Dec 13, 2017 at 09:13 AM ·
c#scripting problemgunno error
fully automatic gun script not working
so I have a gun script attached to a gun GameObject. My goal is to have a gun that when you hold down the mouse it shoots rays at firerate/Time.time . Unfortunately, I can not make this work. I have followed the code from a tutorial. What happens is the first time I hold the mouse down, three bullets are fired. Then two, then one. After that nothing happens. I am getting no errors. I do not know what is wrong, Can someone help please? Code:`using UnityEngine; using System.Collections;
public class Gun : MonoBehaviour {
public float fireRate = 15f;
private float nextTimeToFire = 0f;
public Camera cam;
public ParticleSystem muzzleflash;
public AudioClip Gunshot;
public GameObject impact;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire += Time.time + 1 / fireRate;
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit))
{
AudioSource.PlayClipAtPoint(Gunshot, transform.position);
muzzleflash.Play();
GameObject FLASH = Instantiate(impact, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(FLASH, 0.1f);
}
}
}`
Comment