- Home /
How to make my gun semi-auto?
I want to know how to make my gun semi auto instead of full automatic? I've tried to use a boolean to turn of the inovake but it did not work. I try to make a shotgun/sniper/rifle and wonder if anyone knows how to make it? Here's the script:
#pragma strict
var reloadSound: AudioClip;
var fireSound: AudioClip;
var NoAmmoSound : AudioClip;
var bullet : GameObject;
var scope: GameObject;
var laser: GameObject;
var silencer: GameObject;
private var hasScope: boolean;
private var hasLaser: boolean;
private var hasSilencer: boolean;
var bulletsPerSecond = 14.0;
private var shooting = false;
var bulletForce = 1000.0;
var bulletsInMag = 30;
var magsLeft = 10;
var LoadAmmount = 30;
private var reloading = false;
var reloadCount = 0;
var otherObj : GameObject;
var MainCamera : Transform;
var style : GUIStyle;
function Start() {
hasScope = Random.value < 0.35; // scope chance: 20%
hasLaser = Random.value < 0.25; // laser chance: 35%
hasSilencer = Random.value < 0.15; // silencer chance: 25%
scope.SetActiveRecursively(hasScope);
laser.SetActiveRecursively(hasScope);
silencer.SetActiveRecursively(hasScope);
}
{
InvokeRepeating("Shoot", 0.0, 1.0 / bulletsPerSecond);
}
function Shoot() {
if (!shooting) return;
var go = Instantiate(bullet, transform.position, transform.rotation);
go.rigidbody.AddRelativeForce(Vector3.up * bulletForce);
audio.PlayOneShot(fireSound);
bulletsInMag--;
}
function Update(){
shooting = false;
Debug.Log(bulletsInMag);
if(Input.GetAxis("Fire1") && bulletsInMag > 0 && magsLeft > -1 && !reloading){
shooting = true;
}else if(bulletsInMag <= 0 && magsLeft <= 0 && Input.GetMouseButton(0)){
audio.PlayOneShot(NoAmmoSound);
}else if(bulletsInMag <= 0 && magsLeft > 0){
shooting = false;
Reload();
}
}
function Reload(){
reloading = true;
otherObj.GetComponent(Animations).enabled = false;
otherObj.animation.CrossFade ("Reload");
audio.PlayOneShot(reloadSound);
bulletsInMag = LoadAmmount;
magsLeft -= 1;
yield WaitForSeconds(2);
reloading = false;
otherObj.GetComponent(Animations).enabled = true;
}
function OnGUI(){
GUI.Box (Rect (Screen.width - 300,Screen.height-35,200,80), bulletsInMag.ToString(), style);
GUI.Label (Rect (Screen.width - 200,Screen.height-35,200,80), magsLeft.ToString(), style);
}
I ment 2 ways and the second way is to make fire rate varieble and the decrease when you press a button that enable semi auto
Your welcome tell me if it works if not ill get something else
Answer by arain55 · Aug 18, 2014 at 11:24 AM
Just a suggestion
you can do this to ways one way you can make a new if statement saying that if you press not hold the left mouse button you shoot which would mean that you have to press it once to shoot eg..
If(Input.GetKey(KeyCode.C)) // this is the key that enables semi auto
{
If(Input.GetButtonDown("fire1")){ //the buttondown part is important
//then put you shoot paragraph here
}
}
I may have some mistakes since I wrote it on my phone
Your answer
![](https://koobas.hobune.stream/wayback/20220613160857im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# How to have weapon pickup 1 Answer
Sniper bullets not working 0 Answers
FPS - Gun Delay while Rotating 0 Answers
My gun wont fire ! 1 Answer