Old and already answered enough
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.
If I may be so bold as to suggest not using an actual instantiated object for this. It's moving so fast that a Raycast might be a much better option.
I copied the first script and tried it out and it works perfectly, better then most ive used before
this isnt an answer but when i fire the bullet dosent come out of the gun it just stays on the ground why?
Answer by SaiNagata · Jul 19, 2013 at 09:12 AM
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);
}
wouldn't it be - var Bullet : GameObject; var Spawn : GameObject;
that would be for the vars wouldn't it not Transform?
this is better: 'public var Bullet : Transform; public var Spawn : Transform; public var shootSpeed : float; public var AmmoText : UI.Text; public var ClipText : UI.Text; public var Ammo; public var Clips; public var enoughClips; public var enoughAmmo;
function Start() { Clips = 5; Ammo = 10; enoughAmmo = true; enoughClips = true; }
function Update () { if (Ammo <= 0) { enoughAmmo = false; } if(Input.GetButtonDown("Fire1") && enoughAmmo) { Shot(); Ammo = Ammo - 1; } if (Clips <= 0) { enoughClips = false; } if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R) && enoughClips && Ammo < 10) { Clips = Clips - 1; Ammo = 10; enoughAmmo = true; } }
function Shot() { var pel = Instantiate(Bullet, Spawn.position, Spawn.rotation); pel.GetComponent.().AddForce(transform.forward * shootSpeed);
} function FixedUpdate() { AmmoText.text = "Ammo: " + Ammo.ToString(); ClipText.text = "Clips: " + Clips.ToString(); } '
Doesn't work for me. Bullets spawn but only change to the X rotation.
Answer by RetepTrun · Feb 24, 2013 at 12:17 AM
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 tried the shooting script and it worked perfectly, the bullet shot out in front of the muzzle and went straight where ever i shot the gun. There is nothing wrong with the script, just the way you might have been using it.
Answer by JMgames · Nov 11, 2014 at 11:20 PM
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;
}
}
}
Answer by Plazmoid · Oct 05, 2016 at 04:09 PM
Maybe you're using C# script, but this is Java? (I said MAYBE, so don't hate)
Follow this Question
Related Questions
Damage drop-off over distance on a physical projectile 1 Answer
how to add shoot cooldown 1 Answer
How to script shooting to trigger shooting sound 1 Answer
FPS Gun Hold 0 Answers
Raycast Weapon not cause damage 1 Answer