Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by DubiDuboni, Jun 2, 2020.

  1. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    This is my script now :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ScifiEffects : MonoBehaviour
    7. {
    8.     public GameObject spawnEffect;
    9.     public bool automaticFire = false;
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         if (automaticFire == false)
    21.         {
    22.             if (Input.GetMouseButtonDown(0))
    23.             {
    24.                 GameObject effect = Instantiate(spawnEffect, transform.position, Quaternion.FromToRotation(Vector3.left, spawnEffect.transform.forward)) as GameObject;
    25.                 Rigidbody rb = effect.GetComponent<Rigidbody>();
    26.                 var vc = rb.velocity;
    27.                 vc.z = 0;
    28.                 vc.y = 0;
    29.                 rb.velocity = effect.transform.forward * 40;
    30.                 Destroy(effect, 0.5f);
    31.             }
    32.         }
    33.         else
    34.         {
    35.             GameObject effect = Instantiate(spawnEffect, transform.position, Quaternion.FromToRotation(Vector3.left, spawnEffect.transform.forward)) as GameObject;
    36.             Destroy(effect, 0.5f);
    37.         }
    38.     }
    39. }
    40.  
    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.
     
  2. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    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.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Do you want the projectile to go forward on the world Z axis? Or on the local Z axis of the... gun?
     
    DubiDuboni likes this.
  4. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    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).

     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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.
     
    Last edited: Jun 2, 2020
    DubiDuboni likes this.
  6. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    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.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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.
     
    Last edited: Jun 2, 2020
    DubiDuboni likes this.
  8. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    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 :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6.  
    7. public class ShootBullets : MonoBehaviour
    8. {
    9.     public GameObject bulletPrefab;
    10.     public float bulletSpeed = 100f;
    11.     public bool automaticFire = false;
    12.  
    13.     private List<GameObject> startPositions;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         startPositions = GameObject.FindGameObjectsWithTag("Bullet").ToList();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (automaticFire == false)
    25.         {
    26.             if (Input.GetMouseButtonDown(0))
    27.             {
    28.                 for (int i = 0; i < startPositions.Count; i++)
    29.                 {
    30.                     GameObject bullet = Instantiate(bulletPrefab, startPositions[i].transform.position, Quaternion.identity) as GameObject;
    31.                     Rigidbody bulletRB = bullet.GetComponent<Rigidbody>();
    32.                     bulletRB.AddForce(Vector3.forward * bulletSpeed);
    33.                     Destroy(bullet, 0.5f);
    34.                 }
    35.             }
    36.         }
    37.         else
    38.         {
    39.          
    40.         }
    41.     }
    42. }
    43.  
    44.  
    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.

     
    Last edited: Jun 2, 2020
  9. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    Edited my last comment.
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Just create empty GameObjects, make your list public, and assign them in the inspector. No need to use FindGameObjectsWithTag.
     
    DubiDuboni likes this.
  11. DubiDuboni

    DubiDuboni

    Joined:
    Feb 5, 2019
    Posts:
    131
    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.