Shooting Gun Script

Okay so recently I found this code to make my gun shoot but it doesn’t work. There are no errors listed and I’m using the Cartoon SMG from the Asset Store. Here is the code:

//the bullet we are shooting must have a rigidbody 
var Bullet : Transform;
//the speed the bullet is shot at
var Speed = 16000;
//where the bullet spawns (most likely the tip of the gun)
var spawnPoint : Transform;
//if we shoot like a machinegun or not
var RapidFire = false;
//if we shoot every click or not
var SingleFire = true;
//this is only used if rapid fire is set to true
private var shooting = false;
//RateOfFire private
var Counter = Time.deltaTime;
var RateOfFire = 0.250000;

function FixedUpdate () 
{
//if single fire is set to true
if(SingleFire==true){
//we are using the left mouse button to shoot
if(Input.GetButtonUp("Fire1")){
//we create the bullet
var shot =Instantiate(Bullet, spawnPoint.transform.position, Quaternion.identity); 
//we add the speed 
shot.rigidbody.AddForce(transform.forward * Speed); } }
if(RapidFire ==true){
if(Input.GetButtonDown("Fire1")){
shooting=true;
}
if(Input.GetButtonUp("Fire1")){
shooting=false;
}
if(shooting==true){ Counter += Time.deltaTime;
if(RateOfFire
<
Counter){
var shotRapid =Instantiate(Bullet, spawnPoint.transform.position, 
Quaternion.identity); 
//we add the speed 
shotRapid.rigidbody.AddForce(transform.forward * Speed); 
Counter=0; } } } }

What’s wrong? It worked for the guy on Youtube but not for me.

Try This, Its Work For Me…

var Bullet : Transform;
var Spawn : Transform;


function Update ()
{
	if(Input.GetButtonDown("Fire1"))
	{
		Shot();
	}
}

function Shot()
{
	var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation);
	pel.rigidbody.AddForce(transform.forward * 8000);
	

}

1 It is too hard to read

2 I bet it is not the script but how you have used it, your bullet spawn is probly backwards or your bullet prefab has something wrong with it or something like that

I streamlined the script to shoot full auto only. attach the script to an empty gameobject where you want the bullets to come from.

var bullet : Transform;  //the bullet we are shooting must have a rigidbody
var Speed = 32000; //the speed the bullet is shot at

private var shooting = false; //this is only used if rapid fire is set to true
//RateOfFire private
var Counter = Time.deltaTime;
var RateOfFire = 0.5;

function FixedUpdate (){
	
	if(Input.GetButtonDown("Fire1")){
		shooting=true;
	}
	
	if(Input.GetButtonUp("Fire1")){
		shooting=false;
	}
	
	if(shooting==true){ 
	Counter += Time.deltaTime;
	
	if(RateOfFire < Counter){
	var shotRapid =Instantiate(bullet, transform.position, Quaternion.identity);
	shotRapid.rigidbody.AddForce(transform.forward * Speed);
	Counter=0;
	} 
	
} 

}

Maybe you’re using C# script, but this is Java?
(I said MAYBE, so don’t hate)