- Home /
Want to Fire Machine Gun
Hey, I would like to click with the mouse and say bullets fly out, I want it so tht if I hold down the mouse key alot of machine gun bullets fly out and never end until I let go Whats the scripting name of "left mouse click" also what do I put in Javascript that means "fire until button let go." Something like that.
Answer by spinaljack · Apr 20, 2010 at 07:30 PM
If you use Input.GetKeyDown then it'll only fire once but if you use Input.GetKey then it'll tell you if the key is held down. Depending on how you're making bullets come out it'd be something like:
// Instantiate a rigidbody then set the velocity
var projectile : Rigidbody;
function Update () {
// Ctrl was pressed, launch a projectile
if (Input.GetButton("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}
}
That would make a new bullet every frame. So your rate of fire would be framerate-dependent.
Answer by 3dDude · Apr 21, 2010 at 01:40 PM
or you could use Time.time like :
var fireRate : float = 0.1;
var projectile : Rigidbody;
private var nextFire = 0.0;
function Update () {
if(Input.GetKey("mouse 0")&&Time.time > nextFire){
nextFire = Time.time + fireRate;
var copy = Instantiate(projectile,transform.position,transform.rotation);
copy.rigidbody.velocity = transform.TransformDirection(Vector3.forward);
}
}
i like this code better then Invoke.
Yours is the quickets and easiest method that can be applied and tweaked at will. Thanks a lot!
Answer by Eric5h5 · Apr 21, 2010 at 01:04 AM
Use InvokeRepeating:
var rateOfFire = .1;
function Update () {
if (Input.GetButtonDown("Fire1"))
InvokeRepeating("Shoot", .001, rateOfFire);
if (Input.GetButtonUp("Fire1"))
CancelInvoke("Shoot");
}
function Shoot () {
// Instantiate a bullet here
}
Answer by lion-gv · Nov 10, 2010 at 12:23 AM
Here's my solution for rate of fire:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float rateOfFire;
public GameObject primaryProjectile;
private float rateOfFireTimer = 0.0f;
void Update () {
//this is a counter that tells you the time in seconds from the last projectile fired
rateOfFireTimer += Time.deltaTime;
//this bit isn't necessary but I did it for debugging purposes
print (rateOfFireTimer);
//Fire Primary Weapon. I do (1/rateOfFire) so that you can enter a number like 10 in the inspector to mean 10 per second
if (Input.GetButton ("Fire1") && rateOfFireTimer >= (1/rateOfFire))
FirePrimaryWeapon();
}
void FirePrimaryWeapon() {
//this fires the projectile
Instantiate (primaryProjectile, transform.position, Quaternion.identity);
//this resets the timer
rateOfFireTimer = 0.0f;
}
Your answer
Follow this Question
Related Questions
How do I make a gun project a particle? 1 Answer
Limited Fire Rate? 1 Answer
Shooting script not working properly 3 Answers
How to add a delay to my script. 3 Answers
Using the arrow keys to shoot using c# 2 Answers