- Home /
How can I convert this script to automatic fire?
I have this script that I am using for rifles and pistols. But I would like it to work in automatic fire. None of my attempts so far have worked. Please could someone please add it in without making it too complicated. Here is the script
#pragma strict
var Effect : Transform;
var TheDammage = 100;
var gunshotSound : AudioClip;
var reloadGrenade : AudioClip; //sound clip to reload our grenade launcher
var ammoCount : int = 10; //how much ammo in our clip
var totalAmmo : int = 120; //how much total ammo we have available
var reloadTime : int = 3; //how long it takes before we've reloaded
var reloadAmount : int = 10; //how many grenades we'll reload
var ammo : GUIText; //our GUI text that displays our ammo in text format
var fireRate : float = 3.0;
var bulletHole : Material;
var force = 10;
private var nextFire : float =0.0;
function Update ()
{
Fire ();
Reload();
OnGUI();
}
function Fire () {
if (ammoCount > 0) {
if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
nextFire = Time.time + fireRate;
InstantiateGrenade();
ammoCount -=1;
}
}
}
//---------------------------------------------------------------------------------
function InstantiateGrenade () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.GetMouseButtonDown(0))
{
AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 1);
if (Physics.Raycast (ray, hit, 500))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(bulletHole, hit.point, hitRotation);
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
}
//---------------------------------------------------------------------------------
function Reload () {
if(ammoCount < 1 && totalAmmo > 10) { //if we have less than 1 bullet then we can reload
if(Input.GetKeyDown("r")) {
AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1); //plays reload soundclip
yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
ammoCount += reloadAmount; //adds ammo to our "clip" based off the reloadAmount
totalAmmo -= reloadAmount; //subtracts whatever the reloadAmount was from our total ammo every time we reload
}
}
}
//---------------------------------------------------------------------------------
function OnGUI () {
ammo.text = "Ammo: " + ammoCount + "/" + totalAmmo.ToString(); //displays our GUI Text in the format we want. The sections in "" will appear exactly as their written
}
Answer by Stormy102 · Apr 05, 2014 at 10:50 AM
Thanks. I tried that, but the rate of fire was too fast. It blew all of my rounds in a few seconds. Is there anything that could slow it down?
I managed to get it working. I simply tweaked it a litle bit. To change the rate of fire, modify the "fireRate" variable. If anyone wants the whole script for an automatic raycasting script, here it is:
#pragma strict
var Effect : Transform;
var TheDammage = 100;
var gunshotSound : AudioClip;
var reloadGrenade : AudioClip; //sound clip to reload our grenade launcher
var ammoCount : int = 47; //how much ammo in our clip
var totalAmmo : int = 235; //how much total ammo we have available
var reloadTime : int = 5; //how long it takes before we've reloaded
var reloadAmount : int = 47; //how many grenades we'll reload
var ammo : GUIText; //our GUI text that displays our ammo in text format
var gunName : GUIText;
var fireRate : float = 0.1;
var bulletHole : $$anonymous$$aterial;
var force = 10;
private var nextFire : float =0.0;
public var spreadFactor : float = 0.02;
function Update ()
{
Fire ();
Reload();
OnGUI();
}
function Fire () {
if (Input.GetButton("Fire1") && Time.time > nextFire) {
if (ammoCount > 0) {
nextFire = Time.time + fireRate;
InstantiateGrenade();
ammoCount -=1;
}
}
}
//---------------------------------------------------------------------------------
function InstantiateGrenade () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Input.Get$$anonymous$$ouseButton(0))
{
AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 1);
if (Physics.Raycast (ray, hit, 1000))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(bulletHole, hit.point, hitRotation);
Destroy(particleClone.gameObject, 2);
hit.transform.Send$$anonymous$$essage("ApplyDammage", TheDammage, Send$$anonymous$$essageOptions.DontRequireReceiver);
}
}
}
//---------------------------------------------------------------------------------
function Reload () {
if(ammoCount < 1 && totalAmmo > 10) { //if we have less than 1 bullet then we can reload
if(Input.Get$$anonymous$$eyDown("r")) {
AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1); //plays reload soundclip
yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo
ammoCount += reloadAmount; //adds ammo to our "clip" based off the reloadAmount
totalAmmo -= reloadAmount; //subtracts whatever the reloadAmount was from our total ammo every time we reload
}
}
}
//---------------------------------------------------------------------------------
function OnGUI () {
ammo.text = "Ammo: " + ammoCount + "/" + totalAmmo.ToString(); //displays our GUI Text in the format we want. The sections in "" will appear exactly as their written
gunName.text = "Lewis Gun $$anonymous$$ark I";
}
Answer by roojerry · Apr 04, 2014 at 05:47 PM
Try Input.GetButton instead of GetButtonDown. It will return true as long as the button is held instead of only once like GetButtonDown
Your answer
Follow this Question
Related Questions
Gun Firing help? 2 Answers
How to let my gun shoot ?? 0 Answers
Some issues with weapon switching and reloading? 2 Answers
crosshair scrip not working 1 Answer
Need help with bullet/gun scripting 3 Answers