Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

gun shooting script c#

Discussion in 'Scripting' started by XxSaiFxX, Jan 11, 2014.

Thread Status:
Not open for further replies.
  1. XxSaiFxX

    XxSaiFxX

    Joined:
    Jan 6, 2014
    Posts:
    79
    i know this question has been asked before :(

    how do i make my gun shoot it's already a week i learning how to make my gun shoot but none work why ?
    can i have a beginner tutorial of how to make a gun shoot in c#
    thank you
     
    Last edited: Jan 13, 2014
    HeshanYoshitha likes this.
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Dont know, show us what you have already tried then we might be able to help.

    Is this 2D or 3D, Top down, FPS, TPS...

    Need more info.

    Check this out (not my video)
     
    krharris997 and goldygoldbones like this.
  3. XxSaiFxX

    XxSaiFxX

    Joined:
    Jan 6, 2014
    Posts:
    79
    it's 3d fps

    i'll upload the file tmr it's midnight here in hk

    thanks
     
  4. XxSaiFxX

    XxSaiFxX

    Joined:
    Jan 6, 2014
    Posts:
    79
    i tried it just now but i got error:confused: : UnityException: Input Button fire1 is not setup.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShootDemo : MonoBehaviour {
    5.  
    6.     public Rigidbody projectile;
    7.  
    8.     public float speed = 20;
    9.  
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.         if (Input.GetButtonDown("fire1"))
    21.         {
    22.             Rigidbody instantiatedProjectile = Instantiate(projectile,
    23.                                                            transform.position,
    24.                                                            transform.rotation)
    25.                 as Rigidbody;
    26.  
    27.             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
    28.  
    29.         }
    30.     }
    31. }
    Thanks:sad:
     
  5. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    You have to go to Edit -> Project Settings -> Input and add it yourself.

    Edit, in this case change the "fire1" to uppercase might help.
     
  6. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Yes indeed, it should be Fire1

    Code (csharp):
    1. if (Input.GetButtonDown("Fire1"))

    This should work:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShootDemo : MonoBehaviour
    5. {
    6.     public Rigidbody projectile;
    7.     public float speed = 20;
    8.  
    9.     // Update is called once per frame
    10.     void Update ()
    11.     {
    12.         if (Input.GetButtonDown("Fire1"))
    13.         {
    14.             Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
    15.             instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
    16.         }
    17.     }
    18. }
     
    Last edited: Jan 12, 2014
  7. XxSaiFxX

    XxSaiFxX

    Joined:
    Jan 6, 2014
    Posts:
    79
    Thank you so much both of you!
    it helped me alot!
    Thanks.;)
     
  8. sherpaorc

    sherpaorc

    Joined:
    Aug 9, 2014
    Posts:
    1
    nope i typed it in and it gave me hundreds of errors like: [';' expected insert semicolon at end] [expecting EOF, found ';'] and [unexpected token: float]-[unexpected token: public
     
    ishaanxd0 likes this.
  9. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    post the code you have then.
     
  10. micky2171

    micky2171

    Joined:
    Jan 13, 2014
    Posts:
    5
    Hey, this script make my bullet go half way down the character. Is there anything I can do to fix it?
     
    samson98ulrich likes this.
  11. NickHaldon

    NickHaldon

    Joined:
    Mar 19, 2014
    Posts:
    128
    advik12 and pedromanhaes32 like this.
  12. SciFi_muffins1

    SciFi_muffins1

    Joined:
    Dec 2, 2014
    Posts:
    4
    HI I'm ttying to make a really awesome and exciting game in TPS how would I make animations, Gun fire, and enemy AI?
     
  13. GameForLife

    GameForLife

    Joined:
    Aug 17, 2013
    Posts:
    41
    This is my script for C#. Try this.
    Also, make sure you add a spawn point and rigidbody to your bullet prefab.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShootMissle : MonoBehaviour {
    5.    
    6.     public GameObject spawnPoint;
    7.     public GameObject ExplosionPrefab;
    8.     public Rigidbody projectilePrefab;
    9.    
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.        
    19.         if(Input.GetButtonDown("Fire1"))
    20.         {
    21.             Rigidbody hitPlayer;
    22.             hitPlayer = Instantiate(projectilePrefab, transform.position, transform.rotation) as Rigidbody;
    23.             hitPlayer.velocity = transform.TransformDirection(Vector3.forward * 100);
    24. //            Physics.IgnoreCollision ( projectilePrefab.collider, transform.root.collider );
    25.  
    26.    
    27.          }
    28.  
    29.    
    30.         for(var i =0; i < Input.touchCount; ++i)
    31.         {
    32.             if(Input.GetTouch(i).phase == TouchPhase.Began )
    33.             {
    34.             Rigidbody clone;
    35.             clone = Instantiate(projectilePrefab, transform.position, transform.rotation) as Rigidbody;
    36.             clone.velocity = transform.TransformDirection(Vector3.forward * 200);
    37. //            Physics.IgnoreCollision ( projectilePrefab.collider, transform.root.collider );
    38.        
    39.        
    40.             }
    41.         }
    42.        
    43.        
    44.        
    45.     }
    46. }
     
  14. InstantBlam

    InstantBlam

    Joined:
    Nov 15, 2015
    Posts:
    1
    Thank you people very muchfords for the scripts i am using to create "egglike" game known as great game.
     
  15. Zinosaur_BOSS

    Zinosaur_BOSS

    Joined:
    Jan 19, 2016
    Posts:
    1
    Nice, sound awesome!
     
  16. basimhassan

    basimhassan

    Joined:
    Jan 28, 2016
    Posts:
    3
    i am a beggnier and need to know where to put or place this code or on which game object . need help
    :(:(
     
  17. basimhassan

    basimhassan

    Joined:
    Jan 28, 2016
    Posts:
    3
    of gun shooting . i want a simple gun which shoot bullets
     
  18. EthanDoesGamez

    EthanDoesGamez

    Joined:
    Feb 11, 2016
    Posts:
    1
    yo I'm making a game with a grid terrain and bit-style guns. itll be called Bit-Intruder
     
  19. DennyBoy

    DennyBoy

    Joined:
    Jan 19, 2016
    Posts:
    34
    I tried your code (it is working) but the bullet is shooting up! how can i fix it :O thanks a lot!
     
  20. Ahmed-the-gamer

    Ahmed-the-gamer

    Joined:
    Feb 8, 2016
    Posts:
    1
    try rotating the object that has the script applied to
     
  21. hassambuttq7

    hassambuttq7

    Joined:
    Mar 8, 2017
    Posts:
    2
    Instead of using GetButtonDown use GetKeyDown("z")
     
  22. Jarrah_tree

    Jarrah_tree

    Joined:
    Jan 17, 2017
    Posts:
    1
    Just put it on an empty object and then child that to your weapon
     
  23. xYz23

    xYz23

    Joined:
    May 15, 2017
    Posts:
    2
    hello i am beginner at unity i try to make a script npc shoot at player, but when i play my NPC keep shooting up need help
     

    Attached Files:

  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Gumpy54 likes this.
  25. deba124

    deba124

    Joined:
    Feb 20, 2018
    Posts:
    3
    better use: Input.GetButtonDown(0);
     
  26. belcheredavid02

    belcheredavid02

    Joined:
    Jan 5, 2018
    Posts:
    5
    I have tried that and unless they updated it since 2 days ago it wont work on the latest Unity
     
  27. The_Devils_Games

    The_Devils_Games

    Joined:
    Aug 15, 2019
    Posts:
    2
    Im using this to make a shoot but it won't move anyone help me?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Shooting : MonoBehaviour {

    public GameObject bullet;
    public float speed = 100f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    if(Input.GetKeyDown(KeyCode.Space)){
    GameObject instBullet = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;
    Rigidbody instBulletRigidbody = instBullet.GetComponent<Rigidbody>();
    instBulletRigidbody.AddForce(Vector3.forward * speed);

    }

    }
    }
     
    david_velden9 likes this.
  28. chapstickchap

    chapstickchap

    Joined:
    Aug 23, 2019
    Posts:
    1
    I cant combine my bullet and the script what should I do
     
  29. Mehreen_Miana

    Mehreen_Miana

    Joined:
    Sep 13, 2019
    Posts:
    1
    change "fire1" to "Fire1" .
    the error is of case the first letter of fire must be capitalized.
     
    Murilo123450 likes this.
  30. unity_EI6jaTJwLizSMA

    unity_EI6jaTJwLizSMA

    Joined:
    Nov 23, 2019
    Posts:
    1
    How do i makes a gun shooting script for a 2d game im makeing and how do i make it do damage to another target
     
  31. keduplays

    keduplays

    Joined:
    Dec 29, 2019
    Posts:
    1
    how can i shoot something from the gun? like a 3D model (bullet or smth)
    i have the scripts right (i think so...) and i have the "Fire1" in the input place but it doesn't let me to
    get a model to shoot (sry if my wording is bad)
     
  32. yung-ber

    yung-ber

    Joined:
    Nov 27, 2019
    Posts:
    2
    for me it was firing to the right of my gun
     
  33. pedromanhaes32

    pedromanhaes32

    Joined:
    Jul 21, 2020
    Posts:
    1
    [QUOTE = "NickHaldon, post: 1791889, membro: 583844"] Para aqueles que estão tendo problemas. Aqui está um ativo que possui modelos de armas, inimigos, atualizações de armas e até captadores. Eu comecei a usar-lo e é realmente bom.

    https://www.assetstore.unity3d.com/en/#!/content/2561 [/ QUOTE]
    Nossa muito obgd eu tava criando um do zero, ai vi esse me amarrei amigao
     
  34. Hi_DO_You_Sell_Pizza

    Hi_DO_You_Sell_Pizza

    Joined:
    Aug 4, 2020
    Posts:
    1
    i could not child the script to an empy object pls help me
     
  35. GGShay

    GGShay

    Joined:
    Oct 20, 2019
    Posts:
    1
  36. alexle042010

    alexle042010

    Joined:
    Jul 6, 2020
    Posts:
    1
    how can i make that gun have a firerate so that it doesn't spam bullets every time?
     
  37. N1NNIE

    N1NNIE

    Joined:
    Nov 3, 2020
    Posts:
    8
    Hi! I'm trying to make my gun do a 360 ,so I can shoot where my mouse is located. Please help.
     
  38. N1NNIE

    N1NNIE

    Joined:
    Nov 3, 2020
    Posts:
    8
    Also, I need a script where my bullet can hit the enemy and deal damage to it.
     
  39. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @N1NNIE

    This thread is from 2014...

    And both of the things you ask have been answered several times, and you'll find plenty of tutorials on these topics.
     
    riwinger likes this.
  40. N1NNIE

    N1NNIE

    Joined:
    Nov 3, 2020
    Posts:
    8
  41. N1NNIE

    N1NNIE

    Joined:
    Nov 3, 2020
    Posts:
    8
  42. N1NNIE

    N1NNIE

    Joined:
    Nov 3, 2020
    Posts:
    8
    Answer this: Does my gun have to be a 2D object or a 3D object or none?
     
  43. N1NNIE

    N1NNIE

    Joined:
    Nov 3, 2020
    Posts:
    8
    I have a problem. I got my script down where the gun rotates but the bullet doesn't rotate when its under the player. Help please Thx!
     
  44. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,355
    Please stop proving that you are not listening to suggestions to STOP POSTING TO A SIX YEAR OLD THREAD!

    Start your own topic, a NEW topic, and explain your problem correctly. Here's how:

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220
     
  45. riwinger

    riwinger

    Joined:
    Jun 5, 2020
    Posts:
    1
    You need to give Fire1 a capital.
     
  46. warman88

    warman88

    Joined:
    Dec 27, 2020
    Posts:
    6
    Just use GetKeyDown(“z”)
    *you can use any button it doesn’t have to be z
     
  47. warman88

    warman88

    Joined:
    Dec 27, 2020
    Posts:
    6
    GetKeyDown is a lot easier than Fire1 you can use whatever button in GetKeyDown(“whatever button u want”)
     
  48. THEGAMINGGAMERGUY

    THEGAMINGGAMERGUY

    Joined:
    Jan 20, 2021
    Posts:
    6
  49. THEGAMINGGAMERGUY

    THEGAMINGGAMERGUY

    Joined:
    Jan 20, 2021
    Posts:
    6
    it's just not working
     
  50. THEGAMINGGAMERGUY

    THEGAMINGGAMERGUY

    Joined:
    Jan 20, 2021
    Posts:
    6
    it gave me this after I fixed ("fire1") to (Fire1") The variable projectile of shooting has not been assigned I don't know what to do plz help me
     
Thread Status:
Not open for further replies.