Ways to Unity 5: Fire a bullet (fire objects when you press a key)

Well, We need to make a bullet first 

A bullet is an object. This object has the feature of going straight forward at a very high speed.

So we need to create an object and make it move forward as time goes by.

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

public class MoveForward : MonoBehaviour
{
    public float speed = 40f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
    }
}

We make a feature to an object by attaching a script to it.

Then to better combine this feature and that object, we need to make a prefab out of them. Well, the method is quite simple, you just have to drag your object from the Hierarchy window to the Project window, then you got a prefab.

You could use your prefab as easy as dragging it to your Scene when your game is running. But we have a better way to do that: Instantiation.

Fire an object when you press a key

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

public class PlayerController : MonoBehaviour
{
    public GameObject prefab;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) {
            Instantiate(prefab, transform.position, prefab.transform.rotation);
        }
    }
}

Author

echo "aHR0cHM6Ly95aW5nc2hhb3hvLmJsb2dzcG90LmNvbS8yMDIwLzEyL3dheXMtdG8tdW5pdHktNS1maXJlLWJ1bGxldC1maXJlLmh0bWwK" | base64 -D