How can I make a simple shooting script (projectile) ?

This is my script now :

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

public class ScifiEffects : MonoBehaviour
{
    public GameObject spawnEffect;
    public bool automaticFire = false;

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

    // Update is called once per frame
    void Update()
    {
        if (automaticFire == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                GameObject effect = Instantiate(spawnEffect, transform.position, Quaternion.FromToRotation(Vector3.left, spawnEffect.transform.forward)) as GameObject;
                Rigidbody rb = effect.GetComponent<Rigidbody>();
                var vc = rb.velocity;
                vc.z = 0;
                vc.y = 0;
                rb.velocity = effect.transform.forward * 40;
                Destroy(effect, 0.5f);
            }
        }
        else
        {
            GameObject effect = Instantiate(spawnEffect, transform.position, Quaternion.FromToRotation(Vector3.left, spawnEffect.transform.forward)) as GameObject;
            Destroy(effect, 0.5f);
        }
    }
}

When I click the mouse left button the bullets(sphere) are get shot to any direction 360 degrees.
I want it to be shot to one direction on the blue axis I think to move forward straight forward.
Then when it will collide to destroy it. but for now I can’t make it moving forward like shooting a bullet forward. and I’m not sure if using a rigidbody is a good idea at all.

What I did now is in the editor in the Rigidbody I changed the settings of the Constrints and Freezed all the rotations x,y,z and Freezed the Y and Z on the position.

but it’s not moving on the blue axis forward it’s moving on the red axis.

Do you want the projectile to go forward on the world Z axis? Or on the local Z axis of the… gun?

1 Like

Here is a screenshot of my gun and the bullet is a simple sphere :

The gun have 3 holes I did already a script that rotate the gun nonstop and I want to shoot from the 3 holes the sphere as bullets so they move forward where the gun is pointing. The bullets should get out shot from the holes forward.

The sphere have a rigidbody attached.
The gun is part of a Drone and what I’m rotating is the gun barrel object (Gun_barrel).

Do you have a reference to the gun barrel in your script? Can you get one? You would want to use the gunbarrel’s transform.forward vector as the direction of the bullet. That is the z axis for the gun barrel.

1 Like

The script that shoot the bullet should be attached to the Sphere(bullet) ?
and for reference of the gun barrel I just add to the top of the shooting script GameObject gunBarrel
and then just drag the gun barrel to it.

but how should I position the bullets to come out the 3 holes ? Should I manually first time duplicate and position 3 spheres(bullets) in the 3 holes ? There is no reference to the holes only to the gun barrel.

No way! It should be attached to the gun (or barrel). That script just needs a reference to the bullet prefab so it can spawn clones of it with Instantiate (as you are doing).

Create empty GameObjects at the tips of each barrel that are children of the gun. Then in your gun script have three Transform references for Barrel1, Barrel2, Barrel3 (or a Transform[ ] called Barrels). Then your script can choose which barrel to shoot from, and use that as the position to Instantiate the bullet.

1 Like

What I did is positioned 3 bullets just to get the start positions when shooting in the 3 holes. This 3 bullets don’t have rigidbody they are just for finding the start shotting positions. The 3 bullets are children of the gun barrel so they will rotate with it.

Then I created a prefab bullet. The prefab have a rigidbody.

The script is attached to the rotating barrel.
This is the script :

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

public class ShootBullets : MonoBehaviour
{
    public GameObject bulletPrefab;
    public float bulletSpeed = 100f;
    public bool automaticFire = false;

    private List<GameObject> startPositions;

    // Start is called before the first frame update
    void Start()
    {
        startPositions = GameObject.FindGameObjectsWithTag("Bullet").ToList();
    }

    // Update is called once per frame
    void Update()
    {
        if (automaticFire == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                for (int i = 0; i < startPositions.Count; i++)
                {
                    GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, Quaternion.identity) as GameObject;
                    Rigidbody bulletRB = bullet.GetComponent<Rigidbody>();
                    bulletRB.AddForce(Vector3.forward * bulletSpeed);
                    Destroy(bullet, 0.5f);
                }
            }
        }
        else
        {
          
        }
    }
}

but it’s not shooting anything from the 3 holes. no errors but it’s not shooting anything at least not from the positions of the 3 bullets. maybe it’s creating the bullets somewhere else but I don’t see them.

Two problems :

  1. When getting the 3 bullets children of the barrel all of them have the same position but in the editor they are not in the same position.

  2. It’s not shooting anything it’s creating the bullets to be shot but I don’t see them anywhere.

Edited my last comment.

Just create empty GameObjects, make your list public, and assign them in the inspector. No need to use FindGameObjectsWithTag.

1 Like

This is the result so far. I think I invented a popcorn machine. They are coming out from the right places but they are not coming out like bullets more like popcorns.