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. Dismiss Notice

Movement of a bullet

Discussion in 'Scripting' started by filho_duma_puta, Jun 21, 2014.

  1. filho_duma_puta

    filho_duma_puta

    Joined:
    Jun 21, 2014
    Posts:
    4
    I need help with bullets. I have followed a tutorial which explains how to simulate bullets that are shooted. Unfortunately, I have done exactly what I was seeing but I can't figure out why these bullets are not going ahead when I shoot them.
    Basically, the idea is that when the bullet is instantiated, it continues to translate on the axis Y (or whatever axis, depending on the rotation of the object). This bullet is applied to a "gun" which has a variable of type gameObject, at which I attach the bullet. This gun has also a script, which is responsible for creating the bullet on the volley. The bullet has a rigidbody component (with unchecked "use gravity") to make it collide with other rigidbody components.
    The problem is that this bullet seems to float in the air or seems to make spiral rotations. It has been a long time that I have tried to understand why my bullets are not going ahead. This is making me nervous.
    Please help me with this situation. Thank you.

    This is my code for the gun:
    Code (csharp):
    1.  
    2.     public GameObject bullet;
    3.     //static in this case means that it can be access before an object is instantiated.
    4.     //It behaves like a global variable
    5.     public static int numberOfBullets = 20;
    6.     public GUIText counter;
    7.  
    8.     // Update is called once per frame
    9.     void Update()
    10.     {
    11.         counter.text = "Bullets: " + numberOfBullets.ToString();
    12.  
    13.         if (Input.GetKeyDown(KeyCode.E))
    14.         {
    15.             if (numberOfBullets > 0)
    16.             {
    17.                 Instantiate(bullet, transform.position, transform.rotation);
    18.                 --numberOfBullets;
    19.             }
    20.         }
    21.  
    22.         if (Input.GetKeyDown(KeyCode.R))
    23.         {
    24.             if(numberOfBullets < 20)
    25.                 numberOfBullets += (20 - numberOfBullets);
    26.         }
    27.     }
    28.  
    29.  
    30. //This is my code for the bullet:
    31.     public float speed = 0f;
    32.  
    33.     // Update is called once per frame
    34.     void Update ()
    35.     {
    36.         transform.Translate(0, speed * Time.deltaTime, 0);
    37.     }
     
    Last edited: Jun 21, 2014
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
  3. filho_duma_puta

    filho_duma_puta

    Joined:
    Jun 21, 2014
    Posts:
    4
    If

    I have tried but nothing changes or at least it doesn't solve my problem..
     
  4. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    And I guess you didn't forget to set in your bullet's prefab speed greater than zero?
     
  5. filho_duma_puta

    filho_duma_puta

    Joined:
    Jun 21, 2014
    Posts:
    4
    Yes, I didn't forget to set it. It is set to 100
     
  6. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Aha I see. Then the only problem that is left is that you spawn bullets inside the gun. You need to spawn them outside, so they don't collide with the gun itself.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunScript : MonoBehaviour
    5. {
    6.     public GameObject bullet;
    7.     //static in this case means that it can be access before an object is instantiated.
    8.     //It behaves like a global variable
    9.     public static int numberOfBullets = 20;
    10.     public GUIText counter;
    11.     public float offset = 1.0f;
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         counter.text = "Bullets: " + numberOfBullets.ToString();
    16.         if (Input.GetKeyDown(KeyCode.E)) {
    17.             if (numberOfBullets > 0) {
    18.                 Vector3 pos = transform.position;
    19.                 pos.y += offset;
    20.                 Instantiate(bullet, pos, transform.rotation);
    21.                 --numberOfBullets;
    22.             }
    23.         }
    24.         if (Input.GetKeyDown(KeyCode.R)) {
    25.             if (numberOfBullets < 20)
    26.                 numberOfBullets += (20 - numberOfBullets);
    27.         }
    28.     }
    29. }
     
  7. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Alternatively if spawning bullets outside of the gun makes it look ugly and you don't like to mess with gun's collider and bullet's movement is always straight forward....
    You can enable all 6 constraints in rigidbody of your bullet.
     
  8. filho_duma_puta

    filho_duma_puta

    Joined:
    Jun 21, 2014
    Posts:
    4
    Ok, it works only if my helicopter is not moving at all. If I move it the bullets travel with a spiral route, but I think its due to the unity physic simulations, right?
    But is there a way to simulate a shooting better?

    If I checked the constrains in the rigidbody component, the bullet passes throught other rigidbodies...