How to fling and object?

So I am trying to make a game where you have to run around and shoot people. I want to make a different gun shoot method by flinging an object from a certain point. The certain point should be the end (muzzle) of the gun. If someone could write a script in C# for me, with a bulletShoot function. It would also be nice if you could have the bullet return to the gun in .5 secs. Could you also make it so you can’t shoot again in .5 secs? The shoot function should start when you left click. (Like a normal FPS [first person shooter]). It would also be helpful if there could not be a single raycast involved.

Hi @connorwforman

It’s not quite as simple as whipping up some scripts, but I’ll do my best to help get you started.

The first thing you need to do is create a script and name it GunController, then attach it to the Gun gameobject. Then copy this into the script.

    using UnityEngine;
    
    public GameObject bulletPrefab;
    public GameObject firePoint;
    public float coolDown;

    public class GunController : MonoBehaviour
    
    void Update()
    {
    if(coolDown <= 0)
    { 
          if (Input.GetMouseButtonDown(0))
             {
             ShootBullet();
             }
     }
    else
        { 
          coolDown -= time.Deltatime;
         }
     }
void ShootBullet()
{
    Instantiate (bulletPrefab, firePoint.position, firePoint.rotation);
    coolDown = .5f;
}
}

Create an empty GameObject on the gun, and drag the transform where you want the object to appear at. Drag that empty GameObject into the firePoint slot in the inspector for the script.

Next you’ll need to create some kind of bullet prefab, you can use just a standard mesh sphere. Then create a script named Bullet and copy this script.

using UnityEngine;

public float speed;

public class Bullet : MonoBehaviour

void Update()
{
	transform.Translate(Vector3.forward * (speed * time.deltaTime));
	
}}

And this is just the super basics, you’re not even passing a target to the bullet, just sending it forward. Then you’ll need to use Colliders for collision detection.

Here’s some references that may help you get started.

Thank you so much for your help. I know I heard of it in one of the Unity Tutorials, but I forget what a prefab is. @Skaster87