- Home /
Rapid fire script?
Hello, I using the following script to launch my projectile on my game. When I click down, it fires on projectile. How can I make it so that when I hold down the mouse, the projectile continuous the fire (rapid fire, like a machine gun)? Thank you.
var projectile : Rigidbody; var speed = 20; function Update() { if( Input.GetButtonDown( "Fire1" ) ) { var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation ); instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider ); } }
Answer by Jesus_Freak · Nov 14, 2010 at 08:02 PM
all you need to do is:
instead of
if(Input.GetButtonDown("Fire1"))
you need to do
if(Input.GetButton("Fire1"))
that last one check every frame if you are holding down the Fire1 button (left mouse)
here's what you could try:
var projectile : Rigidbody; var speed = 20; var savedTime = 0;
function Update() {
var seconds : int = Time.time; var oddeven = (seconds % 2); if(oddeven) { Shoot (seconds); }
}
function Shoot(seconds) { if(Input.GetButton( "Fire1" ))
{
var instantiatedProjectile : Rigidbody = Instantiate(
projectile, transform.position, transform.rotation ); instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) ); Physics.IgnoreCollision( instantiatedProjectile. collider, transform.root.collider );
} }
you could play with the values, like the var "oddeven"
or if that isn't what you want, just get rid of the var oddeven, and in the
if(oddeven)
{
Shoot (seconds);
}
change it to
if(seconds)
Thanks. Is there anyway I can slow it down? I want to make it like the projectiles in this game http://www.youtube.com/watch?v=y-_qlayw$$anonymous$$Bs (where there is a little bit of space between them but it is still constant if the button is held down).
that's a little mix of the Tornado Twins' turret control and your script, so you have to test it out and tell me whether it's what you want. if it's still not what you want, then tell me.
Thats still not really working (theres a delay in-between the rapid fire, but an extremely large amount of projectiles are still shot).
Would it be possible to have something like this: http://www.newgrounds.com/portal/view/417593 where the projectiles are shot at a set time interval (like a half second) at a set velocity without the need for mouse input? If mouse input would make that better, I could just make it so that the script is only active when the mouse is held down. Thanks
you could change the timeScale to speed up the shooting process..... but that might influence other time- dependent behaviors, but changing timeScale is a good way to speed up that shooting pprocess. just say in an if statement or else, or else if......
then say Time.timeScale = 1; that sets it back to normal, but anything higher will speed up time- dependent behaviors, decreasing that number makes a slo-mo kind of effect. and 0 of course stops all time dependant behaviors. hope that helps! sorry for that taking so long.
like say in the update function if(oddeven) and say Time.timeScale = 5; to speed up the time behaviors.
Answer by zmar0519 · May 05, 2011 at 11:05 PM
look up Time.time in the documentation. It will give you this, a much simpler solution than the other one.
var projectile : GameObject; var fireRate : float; private var lastFire : float = 0;
function Update() { if(Input.GetButton("Fire1") && Time.time > lastFire) { lastFire = Time.time + fireRate; Instantiate(projectile, transform.position, quaternion.identity); } }
Answer by 3dDude · Jan 03, 2011 at 05:57 PM
If unity3_users script is hard to get working correct you can look at this question: http://answers.unity3d.com/questions/12593/first-person-shooter-help/12619#12619
I gave a script there, And I think it might be a little but easier to use.
3dDude: broken link try this http://answers.unity3d.com/questions/19738/first-person-shooter-help.html . This rapidfire post should also return to searchwords: $$anonymous$$eypress speed control keypress slower once many only once cursor repeat button click how to control keypress to do something only once and then many times.
Answer by ElectricFountain · Jul 05, 2013 at 10:58 PM
ok, so here is my solution for shot timing, muzzle flash timing, and audio timing. This script is in C#, hope you know that language...
using UnityEngine;
using System.Collections;
public class gun : MonoBehaviour {
//Author : Electric Fountain Studios
//Variables
public float bulletsLeft;
public float bulletImpulse = 100.0f;
public float reload;
public float bulletCount = 30;
public float reloadTimer = 0.1f;
public float ShotTimer = 0.01f;
public float bullet = 1.0f;
public float timeToReloadPoint = 0.0f;
public float audioVolume = 5.0f;
public float nextFire = 0.0f;
//Game Objects
public GameObject gun_prefab;
public GameObject muzzle_flash;
public GameObject bullet_prefab;
//Audio
public AudioClip shootSound;
float theBullet;
void Start () {
bulletCount = bulletsLeft;
}
void Update () {
if( Input.GetButton("Fire1") && Time.time > nextFire) {
//Time
nextFire = Time.time + ShotTimer;
//Shooting Action
GameObject theBullet = (GameObject)Instantiate(bullet_prefab, gun_prefab.transform.position, gun_prefab.transform.rotation);
theBullet.rigidbody.AddForce( gun_prefab.transform.forward * bulletImpulse, ForceMode.Impulse);
//M4 audio
audio.PlayOneShot(shootSound , audioVolume );
//M4 Muzzle Flash
Instantiate(muzzle_flash, gun_prefab.transform.position, gun_prefab.transform.rotation);
}
if( Input.GetButtonDown("Fire1") ) {
bulletsLeft = bulletCount - bullet;
}
if(bulletsLeft != timeToReloadPoint ){
print("Need To Reload");
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613083035im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Hold Button Shooting 3 Answers
Pressing 2 Buttons at the Same Time 2 Answers
Erratic mouse Input.GetAxis values on OS X 0 Answers
Switch Between Touch And Mouse Controls 0 Answers