- Home /
FireWeapon.cs not working
I have a script and whenever I hit the "Fire1" button, it doesn't work.
Whats currently happening is its firing one bullet and then firing it again in a second.
Here is my script:
using UnityEngine;
using System.Collections;
public class FireWeapon : MonoBehaviour
{
public float fireRate = 0f;
public Rigidbody bullet;
public float speed = 0f;
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
InvokeRepeating("Fire", 1f, fireRate);
}
}
void Fire ()
{
Rigidbody pel = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
pel.AddForce (transform.forward * speed);
}
}
Because that's exactly what you are telling it to do. http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html
Thanks for that reference @JoshNaylor. Now I have rewritten the script with the CancelInvoke() function but it spawns hundreds of them at a time. Here is the script currently:
using UnityEngine;
using System.Collections;
public class WeaponScript : $$anonymous$$onoBehaviour
{
public Rigidbody bullet;
public int ammo;
public float fireRate = 0f;
public float speed = 0f;
void Update ()
{
if(Input.GetButton("Fire1"))
{
InvokeRepeating("Fire", 0f, fireRate);
}
else if(Input.GetButtonUp("Fire1"))
{
CancelInvoke("Fire");
}
}
void Fire ()
{
Rigidbody pel = Instantiate (bullet, transform.position, transform.rotation) as Rigidbody;
pel.AddForce (transform.forward * speed);
}
}
It's going to spawn hundreds of them because you're doing it in Update, and Update runs every frame, and Unity processes a lot of frames per second.
There's a middle ground that you're getting to. Why not say something like InvokeRepeating("Fire", 0.1f, fireRate)? That effectively means you're firing 10 bullets per second. You can use your InvokeRepeating time parameter to control your weapon's rate of fire. But if you tell it to InvokeRepeating every 0 seconds, that effectively means your weapon has an infinite rate of fire, so that's why Unity is spawning hundreds of them.
Answer by N1warhead · Oct 06, 2014 at 03:21 AM
Something like this will work.
public bool Activate = false;
if(Input.GetButton("Fire1")){
StartCoroutine (Shooting (1.0f));
}
}
IEnumerator Shooting(float waitTime){
Activate = true;
yield return new WaitForSeconds (waitTime);
GameObject clone = Instantiate (FX120P, Barrel.position, Barrel.rotation)as GameObject;
audio.PlayOneShot (ShootSFX);
Activate = false;
}
}
I got that from my code, it should shoot like every 1 second, change to your liking, I hope that works for you! P.S. - Change the FX120P to whatever your gameobject is called.
Answer by MitchWardle180 · Jun 03, 2015 at 01:15 PM
Try adding these to variables to your script
public float coolDownTime = 0.25f;
float timer;
then add this line and make sure it the first line in the update function
timer += Time.deltaTime;
then when you check for input you can also check if the timer is more than the cool down time:
if(Input.GetMouseButton(0) && timer >= coolDownTime){
timer = 0;
Shoot();
}
void Shoot(){
//stop audio source
//set audio source clip to shoot sound
//play audio source
//
//CODE TO INSTATIATE BULLET OR RAYCAST
//
}
Your answer
Follow this Question
Related Questions
C# Switch Weapon 2 Answers
pause for dastardly bannana 1 Answer
Weapon Switching 2 Answers
HDRP Camera weapon layer 1 Answer