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.
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);
}
}
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.
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
}
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;
}