- Home /
How Do I Make My Gun JUST Semi-AutoMatic?
I Have a script set up here.
var projectile : Rigidbody; var speed = 5;
function Update () {
if ( Input.GetButton ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
Destroy (clone.gameObject, 4);
}}
But How Do I make it semi auto? any help please?
Use Input.GetButtonDown () ins$$anonymous$$d. It's only called once per mouse click.
Answer by ExpiredIndexCard · Mar 31, 2013 at 07:03 AM
Change the Input.GetButton
to Input.GetButtonDown
It only checks for when the button gets pressed. Hope that helps :)
Answer by Scribe · Apr 01, 2013 at 12:17 AM
This should work for you:
private var shooting = false;
var shotSpacer = 0.1; //time between shots in burst
var burstSpacer = 0.5; //how long after a firing burst you must wait to fire another
var burstShots = 3; //number of shots in a burst
private var remainingShots : int;
var projectile : Rigidbody; //your bullet object
var speed = 5;
function Update () {
if(Input.GetButtonDown("Fire1") && !shooting){
BurstFire ();
}
}
function BurstFire () {
shooting = true;
remainingShots = burstShots;
InvokeRepeating("Fire", 0, shotSpacer);
yield WaitForSeconds(burstSpacer);
shooting = false;
}
function Fire () {
if(remainingShots > 0){
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
remainingShots --;
}else{
CancelInvoke();
}
}
by default it will fire a burst of 3 bullets but you can change the variables to suit your needs.
Scribe
Answer by ExpiredIndexCard · Mar 31, 2013 at 09:27 PM
canShoot : float = false;
coolDown : float = 0;
coolDownMax : float = 2;
function Update(){
if(coolDown < coolDownMax){
coolDown += Time.deltaTime;
canShoot = false;
}
if(coolDown >= coolDownMax){
canShoot = true;
}
if ( Input.GetButton ("Fire1")) {
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
canShoot = false;
coolDown = 0;
Destroy (clone.gameObject, 4);
}
}
This is untested code but I think it will work. Tell me if there are any errors. Also, the time between each shot is 2 seconds. You can change this by changing the coolDownMax variable. Tell me if you need anything else :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613095420im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Gunfire Help 1 Answer
my bullet holes get stuck in mid air 0 Answers
A node in a childnode? 1 Answer
RayCast Shooting not working 0 Answers