Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Automatic shooting

I am making an FPS so I have a shooting mechanism, however it is single-fire (meaning you cannot hold down the lmb (left mouse button) to shoot constantly). I'm a beginner so I suck at C#, so can you guys modify this code for a assault rifle?

using System.Collections;

using System.Collections.Generic;

using UnityEngine;


public class ShootDemo : MonoBehaviour 



    public Rigidbody projectile;


    public float speed = 500;




    // Use this for initialization

    void Start () {




    // Update is called once per frame

    void Update () {


        if (Input.GetButtonDown("Fire1"))


            Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation)

                as Rigidbody;


            instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));





Any answers will be appreciated greatly.


Best Answer

  • MouledouxMouledoux Member
    Accepted Answer

    GetButtonDown("Fire1") will tell you if the button was pushed down this frame.

    GetButton("Fire1") will tell you if the button is pushed down at all.


  • Ok, it’s 3 am for me so I’ll try it in the morning. Thanks!

Sign In or Register to comment.