Search Unity

Bullets fire at the wrong direction, need help

Discussion in 'Scripting' started by hizral, Jul 23, 2014.

  1. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Hello there,

    I have a problem with my bullets projectile. I used instantiate to call the bullet at the gun muzzle and fire the bullet when it is instantiate. There is no problem with the instantiate and firing the bullet, all went well.

    but as soon as i rotate my gun upward or downward the bullet will fire not according to its rotation, it will still fire straight and not follow the rotation of my gun. why is this happening.

    below is my instantiate code:
    Code (CSharp):
    1. public int health;
    2.     public int currentHealth;
    3.     public int playerDamage;  
    4.     public int bulletScore;
    5.     public int endGame;
    6.  
    7.     public GameObject player1;
    8.     public GameObject player2;
    9.     public GameObject enemyType;
    10.     public Transform leftArm;
    11.     public Transform rightArm;
    12.     public Transform sgBoss;
    13.     public Transform mgBoss;
    14.  
    15.     //Quaternion newQuaternion;
    16.     Quaternion rotation;
    17.     public float rotationSpeed;
    18.  
    19.     public bool canLock;
    20.     public bool canShoot;
    21.     public int shoot;
    22.     public int sgAmount;
    23.  
    24.     float timeBeforeShoot;
    25.     float delay;
    26.  
    27.     int bulletCount;
    28.     bool ok;
    29.     bool ok2;
    30.     bool ok3;
    31.  
    32.     // Use this for initialization
    33.     void Awake ()
    34.     {
    35.         ok = true;
    36.         ok2 = true;
    37.         ok3 = true;
    38.      
    39.         //enemyType = transform.gameObject;
    40.         player1 = GameObject.FindWithTag ("fighterOne");
    41.         player2 = GameObject.FindWithTag ("fighterTwo");
    42.         //newQuaternion = Quaternion.Inverse (Quaternion.LookRotation (Vector3.forward, Vector3.up));
    43.  
    44.         endGame = 0;
    45.         health = currentHealth;
    46.     }
    47.  
    48. if (canShoot)
    49.         {
    50.             timeBeforeShoot += Time.deltaTime;
    51.          
    52.             if (timeBeforeShoot > 3)
    53.             {
    54.                 switch (shoot)
    55.                 {
    56.                     case 1:
    57.                  
    58.                         delay += Time.deltaTime;              
    59.                      
    60.                         if (delay > 0.05f && ok2)
    61.                         {
    62.                             Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), mgBoss.rotation);
    63.                             bulletCount += 1;
    64.                             delay = 0.0f;
    65.                         }
    66.                  
    67.                         if (bulletCount >= 5)
    68.                         {
    69.                             ok2 = false;
    70.                      
    71.                             if (delay > 1)
    72.                             {
    73.                                 shoot += 1;
    74.                                 ok2 = true;
    75.                                 bulletCount = 0;
    76.                                 delay = 0.0f;
    77.                             }
    78.                         }
    79.                  
    80.                         break;
    81.                      
    82.                     case 2:
    83.                      
    84.                         delay += Time.deltaTime;  
    85.                  
    86.                         if (delay > 0.5f && ok3)
    87.                         {
    88.                             Transform bullet;
    89.                             int i;
    90.  
    91.                             for (i = 0; i < sgAmount; i++)
    92.                             {
    93.                                 bullet = Instantiate (sgBoss, leftArm.position, leftArm.rotation) as Transform;
    94.                                 bullet.eulerAngles = new Vector3 (0, 0, -20 + (50 / sgAmount * i));
    95.                             }
    96.                          
    97.                             bulletCount += 1;
    98.                             delay = 0.0f;
    99.                         }
    100.                  
    101.                         if (bulletCount >= 1)
    102.                         {
    103.                             ok3 = false;  
    104.                      
    105.                             if (delay > 1)
    106.                             {
    107.                                 shoot = 0;
    108.                                 ok3 = true;
    109.                                 bulletCount = 0;
    110.                                 delay = 0.0f;
    111.                                 timeBeforeShoot = 0.0f;
    112.                             }
    113.                         }
    114.                  
    115.                         break;
    116.                      
    117.                     default:
    118.                      
    119.                         delay += Time.deltaTime;              
    120.                  
    121.                         if (delay > 0.05f && ok)
    122.                         {
    123.                             Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), mgBoss.rotation);
    124.                             bulletCount += 1;
    125.                             delay = 0.0f;
    126.                         }
    127.                  
    128.                         if (bulletCount >= 5)
    129.                         {
    130.                             ok = false;
    131.                                                  
    132.                             if (delay > 1)
    133.                             {
    134.                                 shoot += 1;
    135.                                 ok = true;
    136.                                 bulletCount = 0;
    137.                                 delay = 0.0f;
    138.                             }
    139.                         }
    140.                      
    141.                         break;
    142.                 }
    143.             }
    144.         }

    and this is my bullet projectile when fired:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BulletsBoss : MonoBehaviour {
    5.  
    6.     public float speed;
    7.  
    8.     public float bulletY;
    9.     public bool mgBoss;
    10.     public bool doneFire;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.         doneFire = false;
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.         transform.Translate (speed * Time.deltaTime, 0.0f, 0.0f, Space.Self);
    22.      
    23.         if (mgBoss && !doneFire)
    24.         {
    25.             transform.Translate (0.0f, Random.Range (-bulletY, bulletY), 0.0f, Space.Self);
    26.             doneFire = true;
    27.         }
    28.     }
    29. }
     
  2. Kalekin

    Kalekin

    Joined:
    Apr 20, 2013
    Posts:
    21
    Well, you are instantiating the bullets at a number of places so without more detail I can't be certain which one is giving you your particular issue. Let's break down each of the instantiates though and we might spot where you're going wrong.

    A (Line 62):
    Code (CSharp):
    1.  Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), mgBoss.rotation);
    Here you are instantiating your mgBoss (a prefab I assume?) using the rotation of the prefab itself, mgBoss.rotation. This will result in your mgBoss bullet having the same rotation every instantiation, which is to say the rotation of the prefab. The possible fix to this would be to use the rotation of the arm you're shooting from instead of the prefab.

    e.g.
    Code (CSharp):
    1. Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), rightArm.rotation);
    B (Line 93):
    Code (CSharp):
    1. bullet = Instantiate (sgBoss, leftArm.position, leftArm.rotation) as Transform;
    2. bullet.eulerAngles = new Vector3 (0, 0, -20 + (50 / sgAmount * i));
    Here you instantiate using the rotation of the leftArm but then overwrite this in the next line by setting the bullets eulerAngle rotation. Is this override of rotation intentional? This one seems to be.

    C (Line 123):
    Code (CSharp):
    1.  Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), mgBoss.rotation);
    This issue is a replica of issue A above and would be fixed the same way.

    Let me know if these help.
     
    LeftyRighty likes this.
  3. hizral

    hizral

    Joined:
    Apr 27, 2010
    Posts:
    568
    Thanks Kalekin, for the problem, manage to solve it.Thanks again
    Code (CSharp):
    1.     Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), rightArm.rotation);
    2.  
    now for the bullet sript, yes it is intentional because the bullet that fired will be in pattern like spread bullet from contra.
    Code (CSharp):
    1.     bullet = Instantiate (sgBoss, leftArm.position, leftArm.rotation) as Transform;
    2.     bullet.eulerAngles = new Vector3 (0, 0, -20 + (50 / sgAmount * i));
    3.  
     
    Last edited: Jul 24, 2014